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 | |
Stefan Weil | 5a61cb6 | 2011-09-08 17:55:32 +0200 | [diff] [blame] | 19 | #include "qemu-common.h" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 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> |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 34 | #include <pthread.h> |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 35 | |
| 36 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 37 | |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 38 | #define NBD_BUFFER_SIZE (1024*1024) |
| 39 | |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 40 | static int sigterm_wfd; |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 41 | static int verbose; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 42 | static char *device; |
| 43 | static char *srcpath; |
| 44 | static char *sockpath; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 45 | |
| 46 | static void usage(const char *name) |
| 47 | { |
| 48 | printf( |
| 49 | "Usage: %s [OPTIONS] FILE\n" |
| 50 | "QEMU Disk Network Block Device Server\n" |
| 51 | "\n" |
Laurent Vivier | c2e2872 | 2010-09-17 20:37:25 +0200 | [diff] [blame] | 52 | " -p, --port=PORT port to listen on (default `%d')\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 53 | " -o, --offset=OFFSET offset into the image\n" |
| 54 | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 55 | " -k, --socket=PATH path to the unix socket\n" |
| 56 | " (default '"SOCKET_PATH"')\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 57 | " -r, --read-only export read-only\n" |
| 58 | " -P, --partition=NUM only expose partition NUM\n" |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 59 | " -s, --snapshot use snapshot file\n" |
| 60 | " -n, --nocache disable host cache\n" |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 61 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
| 62 | " -d, --disconnect disconnect the specified device\n" |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 63 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 64 | " -t, --persistent don't exit on the last connection\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 65 | " -v, --verbose display extra debugging information\n" |
| 66 | " -h, --help display this help and exit\n" |
| 67 | " -V, --version output version information and exit\n" |
| 68 | "\n" |
| 69 | "Report bugs to <anthony@codemonkey.ws>\n" |
Laurent Vivier | c2e2872 | 2010-09-17 20:37:25 +0200 | [diff] [blame] | 70 | , name, NBD_DEFAULT_PORT, "DEVICE"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void version(const char *name) |
| 74 | { |
| 75 | printf( |
ths | 315bc7a | 2008-07-18 18:06:23 +0000 | [diff] [blame] | 76 | "%s version 0.0.1\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 77 | "Written by Anthony Liguori.\n" |
| 78 | "\n" |
| 79 | "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n" |
| 80 | "This is free software; see the source for copying conditions. There is NO\n" |
| 81 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" |
ths | 315bc7a | 2008-07-18 18:06:23 +0000 | [diff] [blame] | 82 | , name); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | struct partition_record |
| 86 | { |
| 87 | uint8_t bootable; |
| 88 | uint8_t start_head; |
| 89 | uint32_t start_cylinder; |
| 90 | uint8_t start_sector; |
| 91 | uint8_t system; |
| 92 | uint8_t end_head; |
| 93 | uint8_t end_cylinder; |
| 94 | uint8_t end_sector; |
| 95 | uint32_t start_sector_abs; |
| 96 | uint32_t nb_sectors_abs; |
| 97 | }; |
| 98 | |
| 99 | static void read_partition(uint8_t *p, struct partition_record *r) |
| 100 | { |
| 101 | r->bootable = p[0]; |
| 102 | r->start_head = p[1]; |
| 103 | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); |
| 104 | r->start_sector = p[2] & 0x3f; |
| 105 | r->system = p[4]; |
| 106 | r->end_head = p[5]; |
| 107 | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); |
| 108 | r->end_sector = p[6] & 0x3f; |
| 109 | r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; |
| 110 | r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; |
| 111 | } |
| 112 | |
| 113 | static int find_partition(BlockDriverState *bs, int partition, |
| 114 | off_t *offset, off_t *size) |
| 115 | { |
| 116 | struct partition_record mbr[4]; |
| 117 | uint8_t data[512]; |
| 118 | int i; |
| 119 | int ext_partnum = 4; |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 120 | int ret; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 121 | |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 122 | if ((ret = bdrv_read(bs, 0, data, 1)) < 0) { |
| 123 | errno = -ret; |
| 124 | err(EXIT_FAILURE, "error while reading"); |
| 125 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 126 | |
| 127 | if (data[510] != 0x55 || data[511] != 0xaa) { |
| 128 | errno = -EINVAL; |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | for (i = 0; i < 4; i++) { |
| 133 | read_partition(&data[446 + 16 * i], &mbr[i]); |
| 134 | |
| 135 | if (!mbr[i].nb_sectors_abs) |
| 136 | continue; |
| 137 | |
| 138 | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { |
| 139 | struct partition_record ext[4]; |
| 140 | uint8_t data1[512]; |
| 141 | int j; |
| 142 | |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 143 | if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) { |
| 144 | errno = -ret; |
| 145 | err(EXIT_FAILURE, "error while reading"); |
| 146 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 147 | |
| 148 | for (j = 0; j < 4; j++) { |
| 149 | read_partition(&data1[446 + 16 * j], &ext[j]); |
| 150 | if (!ext[j].nb_sectors_abs) |
| 151 | continue; |
| 152 | |
| 153 | if ((ext_partnum + j + 1) == partition) { |
| 154 | *offset = (uint64_t)ext[j].start_sector_abs << 9; |
| 155 | *size = (uint64_t)ext[j].nb_sectors_abs << 9; |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | ext_partnum += 4; |
| 160 | } else if ((i + 1) == partition) { |
| 161 | *offset = (uint64_t)mbr[i].start_sector_abs << 9; |
| 162 | *size = (uint64_t)mbr[i].nb_sectors_abs << 9; |
| 163 | return 0; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | errno = -ENOENT; |
| 168 | return -1; |
| 169 | } |
| 170 | |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 171 | static void termsig_handler(int signum) |
| 172 | { |
| 173 | static int sigterm_reported; |
| 174 | if (!sigterm_reported) { |
| 175 | sigterm_reported = (write(sigterm_wfd, "", 1) == 1); |
| 176 | } |
| 177 | } |
| 178 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 179 | static void *show_parts(void *arg) |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 180 | { |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 181 | int nbd; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 182 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 183 | /* linux just needs an open() to trigger |
| 184 | * the partition table update |
| 185 | * but remember to load the module with max_part != 0 : |
| 186 | * modprobe nbd max_part=63 |
| 187 | */ |
| 188 | nbd = open(device, O_RDWR); |
| 189 | if (nbd != -1) { |
| 190 | close(nbd); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 191 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | static void *nbd_client_thread(void *arg) |
| 196 | { |
| 197 | int fd = *(int *)arg; |
| 198 | off_t size; |
| 199 | size_t blocksize; |
| 200 | uint32_t nbdflags; |
| 201 | int sock; |
| 202 | int ret; |
| 203 | pthread_t show_parts_thread; |
| 204 | |
| 205 | do { |
| 206 | sock = unix_socket_outgoing(sockpath); |
| 207 | if (sock == -1) { |
| 208 | if (errno != ENOENT && errno != ECONNREFUSED) { |
| 209 | goto out; |
| 210 | } |
| 211 | sleep(1); /* wait parent */ |
| 212 | } |
| 213 | } while (sock == -1); |
| 214 | |
| 215 | ret = nbd_receive_negotiate(sock, NULL, &nbdflags, |
| 216 | &size, &blocksize); |
| 217 | if (ret == -1) { |
| 218 | goto out; |
| 219 | } |
| 220 | |
| 221 | ret = nbd_init(fd, sock, nbdflags, size, blocksize); |
| 222 | if (ret == -1) { |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | /* update partition table */ |
| 227 | pthread_create(&show_parts_thread, NULL, show_parts, NULL); |
| 228 | |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame^] | 229 | if (verbose) { |
| 230 | fprintf(stderr, "NBD device %s is now connected to %s\n", |
| 231 | device, srcpath); |
| 232 | } else { |
| 233 | /* Close stderr so that the qemu-nbd process exits. */ |
| 234 | dup2(STDOUT_FILENO, STDERR_FILENO); |
| 235 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 236 | |
| 237 | ret = nbd_client(fd); |
| 238 | if (ret) { |
| 239 | goto out; |
| 240 | } |
| 241 | close(fd); |
| 242 | kill(getpid(), SIGTERM); |
| 243 | return (void *) EXIT_SUCCESS; |
| 244 | |
| 245 | out: |
| 246 | kill(getpid(), SIGTERM); |
| 247 | return (void *) EXIT_FAILURE; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 248 | } |
| 249 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 250 | int main(int argc, char **argv) |
| 251 | { |
| 252 | BlockDriverState *bs; |
| 253 | off_t dev_offset = 0; |
| 254 | off_t offset = 0; |
Paolo Bonzini | b90fb4b | 2011-09-08 17:24:54 +0200 | [diff] [blame] | 255 | uint32_t nbdflags = 0; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 256 | bool disconnect = false; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 257 | const char *bindto = "0.0.0.0"; |
Laurent Vivier | c2e2872 | 2010-09-17 20:37:25 +0200 | [diff] [blame] | 258 | int port = NBD_DEFAULT_PORT; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 259 | struct sockaddr_in addr; |
| 260 | socklen_t addr_len = sizeof(addr); |
| 261 | off_t fd_size; |
aliguori | d6aa671 | 2009-01-08 19:34:35 +0000 | [diff] [blame] | 262 | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 263 | struct option lopt[] = { |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 264 | { "help", 0, NULL, 'h' }, |
| 265 | { "version", 0, NULL, 'V' }, |
| 266 | { "bind", 1, NULL, 'b' }, |
| 267 | { "port", 1, NULL, 'p' }, |
| 268 | { "socket", 1, NULL, 'k' }, |
| 269 | { "offset", 1, NULL, 'o' }, |
| 270 | { "read-only", 0, NULL, 'r' }, |
| 271 | { "partition", 1, NULL, 'P' }, |
| 272 | { "connect", 1, NULL, 'c' }, |
| 273 | { "disconnect", 0, NULL, 'd' }, |
| 274 | { "snapshot", 0, NULL, 's' }, |
| 275 | { "nocache", 0, NULL, 'n' }, |
| 276 | { "shared", 1, NULL, 'e' }, |
| 277 | { "persistent", 0, NULL, 't' }, |
| 278 | { "verbose", 0, NULL, 'v' }, |
| 279 | { NULL, 0, NULL, 0 } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 280 | }; |
| 281 | int ch; |
| 282 | int opt_ind = 0; |
| 283 | int li; |
| 284 | char *end; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 285 | int flags = BDRV_O_RDWR; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 286 | int partition = -1; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 287 | int ret; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 288 | int shared = 1; |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 289 | uint8_t *data; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 290 | fd_set fds; |
| 291 | int *sharing_fds; |
| 292 | int fd; |
| 293 | int i; |
| 294 | int nb_fds = 0; |
| 295 | int max_fd; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 296 | int persistent = 0; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 297 | pthread_t client_thread; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 298 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 299 | /* The client thread uses SIGTERM to interrupt the server. A signal |
| 300 | * handler ensures that "qemu-nbd -v -c" exits with a nice status code. |
| 301 | */ |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 302 | struct sigaction sa_sigterm; |
| 303 | int sigterm_fd[2]; |
| 304 | if (qemu_pipe(sigterm_fd) == -1) { |
| 305 | err(EXIT_FAILURE, "Error setting up communication pipe"); |
| 306 | } |
| 307 | |
| 308 | sigterm_wfd = sigterm_fd[1]; |
| 309 | memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
| 310 | sa_sigterm.sa_handler = termsig_handler; |
| 311 | sigaction(SIGTERM, &sa_sigterm, NULL); |
| 312 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 313 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 314 | switch (ch) { |
| 315 | case 's': |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 316 | flags |= BDRV_O_SNAPSHOT; |
| 317 | break; |
| 318 | case 'n': |
Christoph Hellwig | a659979 | 2011-05-17 18:04:06 +0200 | [diff] [blame] | 319 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 320 | break; |
| 321 | case 'b': |
| 322 | bindto = optarg; |
| 323 | break; |
| 324 | case 'p': |
| 325 | li = strtol(optarg, &end, 0); |
| 326 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 327 | errx(EXIT_FAILURE, "Invalid port `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 328 | } |
| 329 | if (li < 1 || li > 65535) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 330 | errx(EXIT_FAILURE, "Port out of range `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 331 | } |
| 332 | port = (uint16_t)li; |
| 333 | break; |
| 334 | case 'o': |
| 335 | dev_offset = strtoll (optarg, &end, 0); |
| 336 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 337 | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 338 | } |
| 339 | if (dev_offset < 0) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 340 | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 341 | } |
| 342 | break; |
| 343 | case 'r': |
Paolo Bonzini | b90fb4b | 2011-09-08 17:24:54 +0200 | [diff] [blame] | 344 | nbdflags |= NBD_FLAG_READ_ONLY; |
Naphtali Sprei | 07108b2 | 2010-03-14 15:19:57 +0200 | [diff] [blame] | 345 | flags &= ~BDRV_O_RDWR; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 346 | break; |
| 347 | case 'P': |
| 348 | partition = strtol(optarg, &end, 0); |
| 349 | if (*end) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 350 | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 351 | if (partition < 1 || partition > 8) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 352 | errx(EXIT_FAILURE, "Invalid partition %d", partition); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 353 | break; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 354 | case 'k': |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 355 | sockpath = optarg; |
| 356 | if (sockpath[0] != '/') |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 357 | errx(EXIT_FAILURE, "socket path must be absolute\n"); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 358 | break; |
| 359 | case 'd': |
| 360 | disconnect = true; |
| 361 | break; |
| 362 | case 'c': |
| 363 | device = optarg; |
| 364 | break; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 365 | case 'e': |
| 366 | shared = strtol(optarg, &end, 0); |
| 367 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 368 | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 369 | } |
| 370 | if (shared < 1) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 371 | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 372 | } |
| 373 | break; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 374 | case 't': |
| 375 | persistent = 1; |
| 376 | break; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 377 | case 'v': |
| 378 | verbose = 1; |
| 379 | break; |
| 380 | case 'V': |
| 381 | version(argv[0]); |
| 382 | exit(0); |
| 383 | break; |
| 384 | case 'h': |
| 385 | usage(argv[0]); |
| 386 | exit(0); |
| 387 | break; |
| 388 | case '?': |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 389 | errx(EXIT_FAILURE, "Try `%s --help' for more information.", |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 390 | argv[0]); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if ((argc - optind) != 1) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 395 | errx(EXIT_FAILURE, "Invalid number of argument.\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 396 | "Try `%s --help' for more information.", |
| 397 | argv[0]); |
| 398 | } |
| 399 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 400 | if (disconnect) { |
| 401 | fd = open(argv[optind], O_RDWR); |
| 402 | if (fd == -1) |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 403 | err(EXIT_FAILURE, "Cannot open %s", argv[optind]); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 404 | |
| 405 | nbd_disconnect(fd); |
| 406 | |
| 407 | close(fd); |
| 408 | |
| 409 | printf("%s disconnected\n", argv[optind]); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame^] | 414 | if (device && !verbose) { |
| 415 | int stderr_fd[2]; |
| 416 | pid_t pid; |
| 417 | int ret; |
| 418 | |
| 419 | if (qemu_pipe(stderr_fd) == -1) { |
| 420 | err(EXIT_FAILURE, "Error setting up communication pipe"); |
| 421 | } |
| 422 | |
| 423 | /* Now daemonize, but keep a communication channel open to |
| 424 | * print errors and exit with the proper status code. |
| 425 | */ |
| 426 | pid = fork(); |
| 427 | if (pid == 0) { |
| 428 | close(stderr_fd[0]); |
| 429 | ret = qemu_daemon(0, 0); |
| 430 | |
| 431 | /* Temporarily redirect stderr to the parent's pipe... */ |
| 432 | dup2(stderr_fd[1], STDERR_FILENO); |
| 433 | if (ret == -1) { |
| 434 | err(EXIT_FAILURE, "Failed to daemonize"); |
| 435 | } |
| 436 | |
| 437 | /* ... close the descriptor we inherited and go on. */ |
| 438 | close(stderr_fd[1]); |
| 439 | } else { |
| 440 | bool errors = false; |
| 441 | char *buf; |
| 442 | |
| 443 | /* In the parent. Print error messages from the child until |
| 444 | * it closes the pipe. |
| 445 | */ |
| 446 | close(stderr_fd[1]); |
| 447 | buf = g_malloc(1024); |
| 448 | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { |
| 449 | errors = true; |
| 450 | ret = qemu_write_full(STDERR_FILENO, buf, ret); |
| 451 | if (ret == -1) { |
| 452 | exit(EXIT_FAILURE); |
| 453 | } |
| 454 | } |
| 455 | if (ret == -1) { |
| 456 | err(EXIT_FAILURE, "Cannot read from daemon"); |
| 457 | } |
| 458 | |
| 459 | /* Usually the daemon should not print any message. |
| 460 | * Exit with zero status in that case. |
| 461 | */ |
| 462 | exit(errors); |
| 463 | } |
| 464 | } |
| 465 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 466 | bdrv_init(); |
| 467 | |
| 468 | bs = bdrv_new("hda"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 469 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 470 | srcpath = argv[optind]; |
| 471 | if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) { |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 472 | errno = -ret; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 473 | err(EXIT_FAILURE, "Failed to bdrv_open '%s'", srcpath); |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 474 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 475 | |
| 476 | fd_size = bs->total_sectors * 512; |
| 477 | |
| 478 | if (partition != -1 && |
| 479 | find_partition(bs, partition, &dev_offset, &fd_size)) |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 480 | err(EXIT_FAILURE, "Could not find partition %d", partition); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 481 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 482 | if (device) { |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 483 | int ret; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 484 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 485 | /* Open before spawning new threads. In the future, we may |
| 486 | * drop privileges after opening. |
| 487 | */ |
| 488 | fd = open(device, O_RDWR); |
| 489 | if (fd == -1) { |
| 490 | err(EXIT_FAILURE, "Failed to open %s", device); |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 491 | } |
| 492 | |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 493 | if (sockpath == NULL) { |
| 494 | sockpath = g_malloc(128); |
| 495 | snprintf(sockpath, 128, SOCKET_PATH, basename(device)); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 498 | ret = pthread_create(&client_thread, NULL, nbd_client_thread, &fd); |
| 499 | if (ret != 0) { |
| 500 | errx(EXIT_FAILURE, "Failed to create client thread: %s", |
| 501 | strerror(ret)); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 502 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 503 | } else { |
| 504 | /* Shut up GCC warnings. */ |
| 505 | memset(&client_thread, 0, sizeof(client_thread)); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 508 | sharing_fds = g_malloc((shared + 1) * sizeof(int)); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 509 | |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 510 | if (sockpath) { |
| 511 | sharing_fds[0] = unix_socket_incoming(sockpath); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 512 | } else { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 513 | sharing_fds[0] = tcp_socket_incoming(bindto, port); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 514 | } |
| 515 | |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 516 | if (sharing_fds[0] == -1) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 517 | return 1; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 518 | max_fd = sharing_fds[0]; |
| 519 | nb_fds++; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 520 | |
Christoph Hellwig | 72aef73 | 2010-09-12 23:42:56 +0200 | [diff] [blame] | 521 | data = qemu_blockalign(bs, NBD_BUFFER_SIZE); |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 522 | if (data == NULL) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 523 | errx(EXIT_FAILURE, "Cannot allocate data buffer"); |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 524 | } |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 525 | |
| 526 | do { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 527 | FD_ZERO(&fds); |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 528 | FD_SET(sigterm_fd[0], &fds); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 529 | for (i = 0; i < nb_fds; i++) |
| 530 | FD_SET(sharing_fds[i], &fds); |
| 531 | |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 532 | do { |
| 533 | ret = select(max_fd + 1, &fds, NULL, NULL, NULL); |
| 534 | } while (ret == -1 && errno == EINTR); |
| 535 | if (ret == -1 || FD_ISSET(sigterm_fd[0], &fds)) { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 536 | break; |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 537 | } |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 538 | |
| 539 | if (FD_ISSET(sharing_fds[0], &fds)) |
| 540 | ret--; |
| 541 | for (i = 1; i < nb_fds && ret; i++) { |
| 542 | if (FD_ISSET(sharing_fds[i], &fds)) { |
| 543 | if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset, |
Paolo Bonzini | b90fb4b | 2011-09-08 17:24:54 +0200 | [diff] [blame] | 544 | &offset, nbdflags, data, NBD_BUFFER_SIZE) != 0) { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 545 | close(sharing_fds[i]); |
| 546 | nb_fds--; |
| 547 | sharing_fds[i] = sharing_fds[nb_fds]; |
| 548 | i--; |
| 549 | } |
| 550 | ret--; |
| 551 | } |
| 552 | } |
| 553 | /* new connection ? */ |
| 554 | if (FD_ISSET(sharing_fds[0], &fds)) { |
| 555 | if (nb_fds < shared + 1) { |
| 556 | sharing_fds[nb_fds] = accept(sharing_fds[0], |
| 557 | (struct sockaddr *)&addr, |
| 558 | &addr_len); |
| 559 | if (sharing_fds[nb_fds] != -1 && |
Paolo Bonzini | b90fb4b | 2011-09-08 17:24:54 +0200 | [diff] [blame] | 560 | nbd_negotiate(sharing_fds[nb_fds], fd_size, nbdflags) != -1) { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 561 | if (sharing_fds[nb_fds] > max_fd) |
| 562 | max_fd = sharing_fds[nb_fds]; |
| 563 | nb_fds++; |
| 564 | } |
| 565 | } |
| 566 | } |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 567 | } while (persistent || nb_fds > 1); |
Herve Poussineau | f8a8324 | 2010-01-24 21:23:56 +0000 | [diff] [blame] | 568 | qemu_vfree(data); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 569 | |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 570 | close(sharing_fds[0]); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 571 | bdrv_close(bs); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 572 | g_free(sharing_fds); |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 573 | if (sockpath) { |
| 574 | unlink(sockpath); |
| 575 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 576 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 577 | if (device) { |
| 578 | void *ret; |
| 579 | pthread_join(client_thread, &ret); |
| 580 | exit(ret != NULL); |
| 581 | } else { |
| 582 | exit(EXIT_SUCCESS); |
| 583 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 584 | } |