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