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