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