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