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