]>
Commit | Line | Data |
---|---|---|
cd831bd7 | 1 | /* |
7a5ca864 FB |
2 | * Copyright (C) 2005 Anthony Liguori <[email protected]> |
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 | |
8167ee88 | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
7a5ca864 FB |
17 | */ |
18 | ||
d38ea87a | 19 | #include "qemu/osdep.h" |
c2a3d7da EB |
20 | #include <getopt.h> |
21 | #include <libgen.h> | |
22 | #include <pthread.h> | |
23 | ||
da34e65c | 24 | #include "qapi/error.h" |
f348b6d1 | 25 | #include "qemu/cutils.h" |
26f54e9a | 26 | #include "sysemu/block-backend.h" |
b3838a40 | 27 | #include "block/block_int.h" |
737e150e | 28 | #include "block/nbd.h" |
6a1751b7 | 29 | #include "qemu/main-loop.h" |
922a01a0 | 30 | #include "qemu/option.h" |
537b41f5 | 31 | #include "qemu/error-report.h" |
0ab3b337 | 32 | #include "qemu/config-file.h" |
58369e22 | 33 | #include "qemu/bswap.h" |
39ca463e | 34 | #include "qemu/log.h" |
53fabd4b | 35 | #include "qemu/systemd.h" |
8c116b0e | 36 | #include "block/snapshot.h" |
452fcdbc | 37 | #include "qapi/qmp/qdict.h" |
d49b6836 | 38 | #include "qapi/qmp/qstring.h" |
0ab3b337 | 39 | #include "qom/object_interfaces.h" |
d0d6ff58 | 40 | #include "io/channel-socket.h" |
e4849c1d | 41 | #include "io/net-listener.h" |
c2297088 | 42 | #include "crypto/init.h" |
39ca463e | 43 | #include "trace/control.h" |
be377133 | 44 | #include "qemu-version.h" |
7a5ca864 | 45 | |
713cc671 | 46 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
fa8b7ce2 DB |
47 | #define QEMU_NBD_OPT_CACHE 256 |
48 | #define QEMU_NBD_OPT_AIO 257 | |
49 | #define QEMU_NBD_OPT_DISCARD 258 | |
50 | #define QEMU_NBD_OPT_DETECT_ZEROES 259 | |
51 | #define QEMU_NBD_OPT_OBJECT 260 | |
52 | #define QEMU_NBD_OPT_TLSCREDS 261 | |
53 | #define QEMU_NBD_OPT_IMAGE_OPTS 262 | |
ffb31e1d | 54 | #define QEMU_NBD_OPT_FORK 263 |
7a5ca864 | 55 | |
bd31c214 EB |
56 | #define MBR_SIZE 512 |
57 | ||
af49bbbe | 58 | static NBDExport *exp; |
3d4b2f9c | 59 | static bool newproto; |
b1d8e52e | 60 | static int verbose; |
a517e88b | 61 | static char *srcpath; |
bd269ebc | 62 | static SocketAddress *saddr; |
7860a380 PB |
63 | static int persistent = 0; |
64 | static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state; | |
a61c6782 PB |
65 | static int shared = 1; |
66 | static int nb_fds; | |
e4849c1d | 67 | static QIONetListener *server; |
145614a1 | 68 | static QCryptoTLSCreds *tlscreds; |
7a5ca864 FB |
69 | |
70 | static void usage(const char *name) | |
71 | { | |
b033cd86 | 72 | (printf) ( |
7a5ca864 FB |
73 | "Usage: %s [OPTIONS] FILE\n" |
74 | "QEMU Disk Network Block Device Server\n" | |
75 | "\n" | |
713cc671 PL |
76 | " -h, --help display this help and exit\n" |
77 | " -V, --version output version information and exit\n" | |
b033cd86 PB |
78 | "\n" |
79 | "Connection properties:\n" | |
713cc671 PL |
80 | " -p, --port=PORT port to listen on (default `%d')\n" |
81 | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" | |
82 | " -k, --socket=PATH path to the unix socket\n" | |
83 | " (default '"SOCKET_PATH"')\n" | |
84 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" | |
85 | " -t, --persistent don't exit on the last connection\n" | |
86 | " -v, --verbose display extra debugging information\n" | |
332a254b | 87 | " -x, --export-name=NAME expose export by name\n" |
b1a75b33 | 88 | " -D, --description=TEXT with -x, also export a human-readable description\n" |
7a5ca864 | 89 | "\n" |
b033cd86 | 90 | "Exposing part of the image:\n" |
713cc671 PL |
91 | " -o, --offset=OFFSET offset into the image\n" |
92 | " -P, --partition=NUM only expose partition NUM\n" | |
b033cd86 | 93 | "\n" |
0ab3b337 DB |
94 | "General purpose options:\n" |
95 | " --object type,id=ID,... define an object such as 'secret' for providing\n" | |
96 | " passwords and/or encryption keys\n" | |
f7812df7 | 97 | " --tls-creds=ID use id of an earlier --object to provide TLS\n" |
39ca463e DL |
98 | " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
99 | " specify tracing options\n" | |
ffb31e1d HR |
100 | " --fork fork off the server process and exit the parent\n" |
101 | " once the server is running\n" | |
b033cd86 PB |
102 | #ifdef __linux__ |
103 | "Kernel NBD client support:\n" | |
713cc671 PL |
104 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
105 | " -d, --disconnect disconnect the specified device\n" | |
b033cd86 PB |
106 | "\n" |
107 | #endif | |
108 | "\n" | |
109 | "Block device options:\n" | |
713cc671 PL |
110 | " -f, --format=FORMAT set image format (raw, qcow2, ...)\n" |
111 | " -r, --read-only export read-only\n" | |
112 | " -s, --snapshot use FILE as an external snapshot, create a temporary\n" | |
113 | " file with backing_file=FILE, redirect the write to\n" | |
114 | " the temporary one\n" | |
8c116b0e | 115 | " -l, --load-snapshot=SNAPSHOT_PARAM\n" |
713cc671 PL |
116 | " load an internal snapshot inside FILE and export it\n" |
117 | " as an read-only device, SNAPSHOT_PARAM format is\n" | |
118 | " 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
119 | " '[ID_OR_NAME]'\n" | |
120 | " -n, --nocache disable host cache\n" | |
121 | " --cache=MODE set cache mode (none, writeback, ...)\n" | |
713cc671 | 122 | " --aio=MODE set AIO mode (native or threads)\n" |
b3838a40 | 123 | " --discard=MODE set discard mode (ignore, unmap)\n" |
6883de6c | 124 | " --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n" |
77c9aaef | 125 | " --image-opts treat FILE as a full set of image options\n" |
b033cd86 | 126 | "\n" |
f5048cb7 | 127 | QEMU_HELP_BOTTOM "\n" |
c2e2872b | 128 | , name, NBD_DEFAULT_PORT, "DEVICE"); |
7a5ca864 FB |
129 | } |
130 | ||
131 | static void version(const char *name) | |
132 | { | |
133 | printf( | |
7e563bfb | 134 | "%s " QEMU_FULL_VERSION "\n" |
7a5ca864 FB |
135 | "Written by Anthony Liguori.\n" |
136 | "\n" | |
be377133 | 137 | QEMU_COPYRIGHT "\n" |
7a5ca864 FB |
138 | "This is free software; see the source for copying conditions. There is NO\n" |
139 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" | |
315bc7aa | 140 | , name); |
7a5ca864 FB |
141 | } |
142 | ||
143 | struct partition_record | |
144 | { | |
145 | uint8_t bootable; | |
146 | uint8_t start_head; | |
147 | uint32_t start_cylinder; | |
148 | uint8_t start_sector; | |
149 | uint8_t system; | |
150 | uint8_t end_head; | |
151 | uint8_t end_cylinder; | |
152 | uint8_t end_sector; | |
153 | uint32_t start_sector_abs; | |
154 | uint32_t nb_sectors_abs; | |
155 | }; | |
156 | ||
157 | static void read_partition(uint8_t *p, struct partition_record *r) | |
158 | { | |
159 | r->bootable = p[0]; | |
160 | r->start_head = p[1]; | |
161 | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); | |
162 | r->start_sector = p[2] & 0x3f; | |
163 | r->system = p[4]; | |
164 | r->end_head = p[5]; | |
165 | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); | |
166 | r->end_sector = p[6] & 0x3f; | |
ac97393d | 167 | |
773dce3c PM |
168 | r->start_sector_abs = ldl_le_p(p + 8); |
169 | r->nb_sectors_abs = ldl_le_p(p + 12); | |
7a5ca864 FB |
170 | } |
171 | ||
4c58e80a | 172 | static int find_partition(BlockBackend *blk, int partition, |
7a5ca864 FB |
173 | off_t *offset, off_t *size) |
174 | { | |
175 | struct partition_record mbr[4]; | |
bd31c214 | 176 | uint8_t data[MBR_SIZE]; |
7a5ca864 FB |
177 | int i; |
178 | int ext_partnum = 4; | |
cb7cf0e3 | 179 | int ret; |
7a5ca864 | 180 | |
bd31c214 EB |
181 | ret = blk_pread(blk, 0, data, sizeof(data)); |
182 | if (ret < 0) { | |
a4699e55 | 183 | error_report("error while reading: %s", strerror(-ret)); |
85b01e09 | 184 | exit(EXIT_FAILURE); |
cb7cf0e3 | 185 | } |
7a5ca864 FB |
186 | |
187 | if (data[510] != 0x55 || data[511] != 0xaa) { | |
185b4338 | 188 | return -EINVAL; |
7a5ca864 FB |
189 | } |
190 | ||
191 | for (i = 0; i < 4; i++) { | |
192 | read_partition(&data[446 + 16 * i], &mbr[i]); | |
193 | ||
453b07b1 | 194 | if (!mbr[i].system || !mbr[i].nb_sectors_abs) { |
7a5ca864 | 195 | continue; |
453b07b1 | 196 | } |
7a5ca864 FB |
197 | |
198 | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { | |
199 | struct partition_record ext[4]; | |
bd31c214 | 200 | uint8_t data1[MBR_SIZE]; |
7a5ca864 FB |
201 | int j; |
202 | ||
bd31c214 EB |
203 | ret = blk_pread(blk, mbr[i].start_sector_abs * MBR_SIZE, |
204 | data1, sizeof(data1)); | |
205 | if (ret < 0) { | |
a4699e55 | 206 | error_report("error while reading: %s", strerror(-ret)); |
85b01e09 | 207 | exit(EXIT_FAILURE); |
cb7cf0e3 | 208 | } |
7a5ca864 FB |
209 | |
210 | for (j = 0; j < 4; j++) { | |
211 | read_partition(&data1[446 + 16 * j], &ext[j]); | |
453b07b1 | 212 | if (!ext[j].system || !ext[j].nb_sectors_abs) { |
7a5ca864 | 213 | continue; |
453b07b1 | 214 | } |
7a5ca864 FB |
215 | |
216 | if ((ext_partnum + j + 1) == partition) { | |
217 | *offset = (uint64_t)ext[j].start_sector_abs << 9; | |
218 | *size = (uint64_t)ext[j].nb_sectors_abs << 9; | |
219 | return 0; | |
220 | } | |
221 | } | |
222 | ext_partnum += 4; | |
223 | } else if ((i + 1) == partition) { | |
224 | *offset = (uint64_t)mbr[i].start_sector_abs << 9; | |
225 | *size = (uint64_t)mbr[i].nb_sectors_abs << 9; | |
226 | return 0; | |
227 | } | |
228 | } | |
229 | ||
185b4338 | 230 | return -ENOENT; |
7a5ca864 FB |
231 | } |
232 | ||
bb345110 PB |
233 | static void termsig_handler(int signum) |
234 | { | |
23994a5f | 235 | atomic_cmpxchg(&state, RUNNING, TERMINATE); |
a61c6782 | 236 | qemu_notify_event(); |
bb345110 PB |
237 | } |
238 | ||
537b41f5 | 239 | |
a517e88b | 240 | static void *show_parts(void *arg) |
cd831bd7 | 241 | { |
a6ac2313 | 242 | char *device = arg; |
a517e88b PB |
243 | int nbd; |
244 | ||
245 | /* linux just needs an open() to trigger | |
246 | * the partition table update | |
247 | * but remember to load the module with max_part != 0 : | |
248 | * modprobe nbd max_part=63 | |
249 | */ | |
250 | nbd = open(device, O_RDWR); | |
fc19f8a0 | 251 | if (nbd >= 0) { |
a517e88b PB |
252 | close(nbd); |
253 | } | |
254 | return NULL; | |
255 | } | |
cd831bd7 | 256 | |
a517e88b PB |
257 | static void *nbd_client_thread(void *arg) |
258 | { | |
a6ac2313 | 259 | char *device = arg; |
081dd1fe | 260 | NBDExportInfo info = { .request_sizes = false, }; |
d0d6ff58 DB |
261 | QIOChannelSocket *sioc; |
262 | int fd; | |
a517e88b PB |
263 | int ret; |
264 | pthread_t show_parts_thread; | |
1ce52846 | 265 | Error *local_error = NULL; |
a517e88b | 266 | |
d0d6ff58 DB |
267 | sioc = qio_channel_socket_new(); |
268 | if (qio_channel_socket_connect_sync(sioc, | |
269 | saddr, | |
270 | &local_error) < 0) { | |
48bec07e | 271 | error_report_err(local_error); |
dc10e8b3 SH |
272 | goto out; |
273 | } | |
a517e88b | 274 | |
004a89fc EB |
275 | ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL, |
276 | NULL, NULL, NULL, &info, &local_error); | |
fc19f8a0 | 277 | if (ret < 0) { |
1ce52846 | 278 | if (local_error) { |
78288671 | 279 | error_report_err(local_error); |
1ce52846 | 280 | } |
0c544d73 | 281 | goto out_socket; |
a517e88b PB |
282 | } |
283 | ||
a6ac2313 | 284 | fd = open(device, O_RDWR); |
fc19f8a0 | 285 | if (fd < 0) { |
a6ac2313 | 286 | /* Linux-only, we can use %m in printf. */ |
b9884681 | 287 | error_report("Failed to open %s: %m", device); |
0c544d73 | 288 | goto out_socket; |
a6ac2313 PB |
289 | } |
290 | ||
004a89fc | 291 | ret = nbd_init(fd, sioc, &info, &local_error); |
fc19f8a0 | 292 | if (ret < 0) { |
be41c100 | 293 | error_report_err(local_error); |
0c544d73 | 294 | goto out_fd; |
a517e88b PB |
295 | } |
296 | ||
297 | /* update partition table */ | |
a6ac2313 | 298 | pthread_create(&show_parts_thread, NULL, show_parts, device); |
a517e88b | 299 | |
c1f8fdc3 PB |
300 | if (verbose) { |
301 | fprintf(stderr, "NBD device %s is now connected to %s\n", | |
302 | device, srcpath); | |
303 | } else { | |
304 | /* Close stderr so that the qemu-nbd process exits. */ | |
305 | dup2(STDOUT_FILENO, STDERR_FILENO); | |
306 | } | |
a517e88b PB |
307 | |
308 | ret = nbd_client(fd); | |
309 | if (ret) { | |
0c544d73 | 310 | goto out_fd; |
cd831bd7 | 311 | } |
a517e88b | 312 | close(fd); |
d0d6ff58 | 313 | object_unref(OBJECT(sioc)); |
a517e88b PB |
314 | kill(getpid(), SIGTERM); |
315 | return (void *) EXIT_SUCCESS; | |
316 | ||
0c544d73 PB |
317 | out_fd: |
318 | close(fd); | |
319 | out_socket: | |
d0d6ff58 | 320 | object_unref(OBJECT(sioc)); |
a517e88b PB |
321 | out: |
322 | kill(getpid(), SIGTERM); | |
323 | return (void *) EXIT_FAILURE; | |
cd831bd7 TS |
324 | } |
325 | ||
e4afbf4f | 326 | static int nbd_can_accept(void) |
a61c6782 | 327 | { |
df8ad9f1 | 328 | return state == RUNNING && nb_fds < shared; |
a61c6782 PB |
329 | } |
330 | ||
7860a380 PB |
331 | static void nbd_export_closed(NBDExport *exp) |
332 | { | |
333 | assert(state == TERMINATING); | |
334 | state = TERMINATED; | |
335 | } | |
336 | ||
d0d6ff58 | 337 | static void nbd_update_server_watch(void); |
e4afbf4f | 338 | |
0c9390d9 | 339 | static void nbd_client_closed(NBDClient *client, bool negotiated) |
a61c6782 | 340 | { |
1743b515 | 341 | nb_fds--; |
0c9390d9 | 342 | if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) { |
7860a380 PB |
343 | state = TERMINATE; |
344 | } | |
d0d6ff58 | 345 | nbd_update_server_watch(); |
7860a380 | 346 | nbd_client_put(client); |
a61c6782 PB |
347 | } |
348 | ||
e4849c1d DB |
349 | static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc, |
350 | gpointer opaque) | |
a61c6782 | 351 | { |
7860a380 | 352 | if (state >= TERMINATE) { |
e4849c1d | 353 | return; |
7860a380 PB |
354 | } |
355 | ||
ee7d7aab | 356 | nb_fds++; |
d0d6ff58 | 357 | nbd_update_server_watch(); |
f95910fe | 358 | nbd_client_new(newproto ? NULL : exp, cioc, |
145614a1 | 359 | tlscreds, NULL, nbd_client_closed); |
a61c6782 PB |
360 | } |
361 | ||
d0d6ff58 | 362 | static void nbd_update_server_watch(void) |
e4afbf4f FZ |
363 | { |
364 | if (nbd_can_accept()) { | |
e4849c1d | 365 | qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL); |
e4afbf4f | 366 | } else { |
e4849c1d | 367 | qio_net_listener_set_client_func(server, NULL, NULL, NULL); |
e4afbf4f FZ |
368 | } |
369 | } | |
370 | ||
48bec07e | 371 | |
bd269ebc | 372 | static SocketAddress *nbd_build_socket_address(const char *sockpath, |
48bec07e DB |
373 | const char *bindto, |
374 | const char *port) | |
375 | { | |
bd269ebc | 376 | SocketAddress *saddr; |
48bec07e | 377 | |
bd269ebc | 378 | saddr = g_new0(SocketAddress, 1); |
48bec07e | 379 | if (sockpath) { |
bd269ebc MA |
380 | saddr->type = SOCKET_ADDRESS_TYPE_UNIX; |
381 | saddr->u.q_unix.path = g_strdup(sockpath); | |
48bec07e | 382 | } else { |
0399293e | 383 | InetSocketAddress *inet; |
bd269ebc MA |
384 | saddr->type = SOCKET_ADDRESS_TYPE_INET; |
385 | inet = &saddr->u.inet; | |
0399293e | 386 | inet->host = g_strdup(bindto); |
48bec07e | 387 | if (port) { |
0399293e | 388 | inet->port = g_strdup(port); |
48bec07e | 389 | } else { |
0399293e | 390 | inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); |
48bec07e DB |
391 | } |
392 | } | |
393 | ||
394 | return saddr; | |
395 | } | |
396 | ||
397 | ||
77c9aaef DB |
398 | static QemuOptsList file_opts = { |
399 | .name = "file", | |
400 | .implied_opt_name = "file", | |
401 | .head = QTAILQ_HEAD_INITIALIZER(file_opts.head), | |
402 | .desc = { | |
403 | /* no elements => accept any params */ | |
404 | { /* end of list */ } | |
405 | }, | |
406 | }; | |
407 | ||
0ab3b337 DB |
408 | static QemuOptsList qemu_object_opts = { |
409 | .name = "object", | |
410 | .implied_opt_name = "qom-type", | |
411 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), | |
412 | .desc = { | |
413 | { } | |
414 | }, | |
415 | }; | |
416 | ||
417 | ||
145614a1 DB |
418 | |
419 | static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) | |
420 | { | |
421 | Object *obj; | |
422 | QCryptoTLSCreds *creds; | |
423 | ||
424 | obj = object_resolve_path_component( | |
425 | object_get_objects_root(), id); | |
426 | if (!obj) { | |
427 | error_setg(errp, "No TLS credentials with id '%s'", | |
428 | id); | |
429 | return NULL; | |
430 | } | |
431 | creds = (QCryptoTLSCreds *) | |
432 | object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); | |
433 | if (!creds) { | |
434 | error_setg(errp, "Object with id '%s' is not TLS credentials", | |
435 | id); | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { | |
440 | error_setg(errp, | |
441 | "Expecting TLS credentials with a server endpoint"); | |
442 | return NULL; | |
443 | } | |
444 | object_ref(obj); | |
445 | return creds; | |
446 | } | |
447 | ||
a721f53b RJ |
448 | static void setup_address_and_port(const char **address, const char **port) |
449 | { | |
450 | if (*address == NULL) { | |
451 | *address = "0.0.0.0"; | |
452 | } | |
453 | ||
454 | if (*port == NULL) { | |
455 | *port = stringify(NBD_DEFAULT_PORT); | |
456 | } | |
457 | } | |
458 | ||
a721f53b RJ |
459 | /* |
460 | * Check socket parameters compatibility when socket activation is used. | |
461 | */ | |
462 | static const char *socket_activation_validate_opts(const char *device, | |
463 | const char *sockpath, | |
464 | const char *address, | |
465 | const char *port) | |
466 | { | |
467 | if (device != NULL) { | |
468 | return "NBD device can't be set when using socket activation"; | |
469 | } | |
470 | ||
471 | if (sockpath != NULL) { | |
472 | return "Unix socket can't be set when using socket activation"; | |
473 | } | |
474 | ||
475 | if (address != NULL) { | |
476 | return "The interface can't be set when using socket activation"; | |
477 | } | |
478 | ||
479 | if (port != NULL) { | |
480 | return "TCP port number can't be set when using socket activation"; | |
481 | } | |
482 | ||
483 | return NULL; | |
484 | } | |
145614a1 | 485 | |
b3b5299d KW |
486 | static void qemu_nbd_shutdown(void) |
487 | { | |
488 | job_cancel_sync_all(); | |
489 | bdrv_close_all(); | |
490 | } | |
491 | ||
7a5ca864 FB |
492 | int main(int argc, char **argv) |
493 | { | |
26f54e9a | 494 | BlockBackend *blk; |
7a5ca864 FB |
495 | BlockDriverState *bs; |
496 | off_t dev_offset = 0; | |
7423f417 | 497 | uint16_t nbdflags = 0; |
cd831bd7 | 498 | bool disconnect = false; |
a721f53b | 499 | const char *bindto = NULL; |
48bec07e DB |
500 | const char *port = NULL; |
501 | char *sockpath = NULL; | |
a6ac2313 | 502 | char *device = NULL; |
7a5ca864 | 503 | off_t fd_size; |
8c116b0e WX |
504 | QemuOpts *sn_opts = NULL; |
505 | const char *sn_id_or_name = NULL; | |
b1a75b33 | 506 | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:x:T:D:"; |
7a5ca864 | 507 | struct option lopt[] = { |
aa6e546c DB |
508 | { "help", no_argument, NULL, 'h' }, |
509 | { "version", no_argument, NULL, 'V' }, | |
510 | { "bind", required_argument, NULL, 'b' }, | |
511 | { "port", required_argument, NULL, 'p' }, | |
512 | { "socket", required_argument, NULL, 'k' }, | |
513 | { "offset", required_argument, NULL, 'o' }, | |
514 | { "read-only", no_argument, NULL, 'r' }, | |
515 | { "partition", required_argument, NULL, 'P' }, | |
516 | { "connect", required_argument, NULL, 'c' }, | |
517 | { "disconnect", no_argument, NULL, 'd' }, | |
518 | { "snapshot", no_argument, NULL, 's' }, | |
519 | { "load-snapshot", required_argument, NULL, 'l' }, | |
520 | { "nocache", no_argument, NULL, 'n' }, | |
521 | { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE }, | |
522 | { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO }, | |
523 | { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD }, | |
524 | { "detect-zeroes", required_argument, NULL, | |
525 | QEMU_NBD_OPT_DETECT_ZEROES }, | |
526 | { "shared", required_argument, NULL, 'e' }, | |
527 | { "format", required_argument, NULL, 'f' }, | |
528 | { "persistent", no_argument, NULL, 't' }, | |
529 | { "verbose", no_argument, NULL, 'v' }, | |
530 | { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT }, | |
531 | { "export-name", required_argument, NULL, 'x' }, | |
b1a75b33 | 532 | { "description", required_argument, NULL, 'D' }, |
aa6e546c DB |
533 | { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS }, |
534 | { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS }, | |
39ca463e | 535 | { "trace", required_argument, NULL, 'T' }, |
ffb31e1d | 536 | { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK }, |
660f11be | 537 | { NULL, 0, NULL, 0 } |
7a5ca864 FB |
538 | }; |
539 | int ch; | |
540 | int opt_ind = 0; | |
7a5ca864 | 541 | char *end; |
f5edb014 | 542 | int flags = BDRV_O_RDWR; |
7a5ca864 | 543 | int partition = -1; |
4fbec260 | 544 | int ret = 0; |
39a5235c | 545 | bool seen_cache = false; |
ded9d2d5 | 546 | bool seen_discard = false; |
39a5235c | 547 | bool seen_aio = false; |
a517e88b | 548 | pthread_t client_thread; |
e6b63677 | 549 | const char *fmt = NULL; |
34b5d2c6 | 550 | Error *local_err = NULL; |
b3838a40 | 551 | BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; |
4fbec260 | 552 | QDict *options = NULL; |
3d4b2f9c | 553 | const char *export_name = NULL; |
b1a75b33 | 554 | const char *export_description = NULL; |
145614a1 | 555 | const char *tlscredsid = NULL; |
77c9aaef | 556 | bool imageOpts = false; |
6effd5bf | 557 | bool writethrough = true; |
39ca463e | 558 | char *trace_file = NULL; |
ffb31e1d HR |
559 | bool fork_process = false; |
560 | int old_stderr = -1; | |
a721f53b | 561 | unsigned socket_activation; |
7a5ca864 | 562 | |
a517e88b PB |
563 | /* The client thread uses SIGTERM to interrupt the server. A signal |
564 | * handler ensures that "qemu-nbd -v -c" exits with a nice status code. | |
565 | */ | |
bb345110 | 566 | struct sigaction sa_sigterm; |
bb345110 PB |
567 | memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
568 | sa_sigterm.sa_handler = termsig_handler; | |
569 | sigaction(SIGTERM, &sa_sigterm, NULL); | |
c2297088 | 570 | |
041e32b8 HR |
571 | #ifdef CONFIG_POSIX |
572 | signal(SIGPIPE, SIG_IGN); | |
573 | #endif | |
574 | ||
fe4db84d | 575 | module_call_init(MODULE_INIT_TRACE); |
e8f2d272 | 576 | qcrypto_init(&error_fatal); |
c2297088 | 577 | |
0ab3b337 DB |
578 | module_call_init(MODULE_INIT_QOM); |
579 | qemu_add_opts(&qemu_object_opts); | |
39ca463e | 580 | qemu_add_opts(&qemu_trace_opts); |
10f5bff6 | 581 | qemu_init_exec_dir(argv[0]); |
bb345110 | 582 | |
7a5ca864 FB |
583 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
584 | switch (ch) { | |
585 | case 's': | |
2f726488 TS |
586 | flags |= BDRV_O_SNAPSHOT; |
587 | break; | |
588 | case 'n': | |
39a5235c PB |
589 | optarg = (char *) "none"; |
590 | /* fallthrough */ | |
591 | case QEMU_NBD_OPT_CACHE: | |
592 | if (seen_cache) { | |
85b01e09 MA |
593 | error_report("-n and --cache can only be specified once"); |
594 | exit(EXIT_FAILURE); | |
39a5235c PB |
595 | } |
596 | seen_cache = true; | |
6effd5bf | 597 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) { |
85b01e09 MA |
598 | error_report("Invalid cache mode `%s'", optarg); |
599 | exit(EXIT_FAILURE); | |
39a5235c | 600 | } |
7a5ca864 | 601 | break; |
39a5235c PB |
602 | case QEMU_NBD_OPT_AIO: |
603 | if (seen_aio) { | |
85b01e09 MA |
604 | error_report("--aio can only be specified once"); |
605 | exit(EXIT_FAILURE); | |
39a5235c PB |
606 | } |
607 | seen_aio = true; | |
608 | if (!strcmp(optarg, "native")) { | |
609 | flags |= BDRV_O_NATIVE_AIO; | |
610 | } else if (!strcmp(optarg, "threads")) { | |
611 | /* this is the default */ | |
612 | } else { | |
85b01e09 MA |
613 | error_report("invalid aio mode `%s'", optarg); |
614 | exit(EXIT_FAILURE); | |
39a5235c PB |
615 | } |
616 | break; | |
ded9d2d5 PB |
617 | case QEMU_NBD_OPT_DISCARD: |
618 | if (seen_discard) { | |
85b01e09 MA |
619 | error_report("--discard can only be specified once"); |
620 | exit(EXIT_FAILURE); | |
ded9d2d5 PB |
621 | } |
622 | seen_discard = true; | |
623 | if (bdrv_parse_discard_flags(optarg, &flags) == -1) { | |
85b01e09 MA |
624 | error_report("Invalid discard mode `%s'", optarg); |
625 | exit(EXIT_FAILURE); | |
ded9d2d5 PB |
626 | } |
627 | break; | |
b3838a40 PL |
628 | case QEMU_NBD_OPT_DETECT_ZEROES: |
629 | detect_zeroes = | |
f7abe0ec | 630 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, |
b3838a40 | 631 | optarg, |
b3838a40 PL |
632 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
633 | &local_err); | |
634 | if (local_err) { | |
c29b77f9 MA |
635 | error_reportf_err(local_err, |
636 | "Failed to parse detect_zeroes mode: "); | |
85b01e09 | 637 | exit(EXIT_FAILURE); |
b3838a40 PL |
638 | } |
639 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
640 | !(flags & BDRV_O_UNMAP)) { | |
85b01e09 MA |
641 | error_report("setting detect-zeroes to unmap is not allowed " |
642 | "without setting discard operation to unmap"); | |
643 | exit(EXIT_FAILURE); | |
b3838a40 PL |
644 | } |
645 | break; | |
7a5ca864 FB |
646 | case 'b': |
647 | bindto = optarg; | |
648 | break; | |
649 | case 'p': | |
48bec07e | 650 | port = optarg; |
7a5ca864 FB |
651 | break; |
652 | case 'o': | |
653 | dev_offset = strtoll (optarg, &end, 0); | |
654 | if (*end) { | |
85b01e09 MA |
655 | error_report("Invalid offset `%s'", optarg); |
656 | exit(EXIT_FAILURE); | |
7a5ca864 FB |
657 | } |
658 | if (dev_offset < 0) { | |
85b01e09 MA |
659 | error_report("Offset must be positive `%s'", optarg); |
660 | exit(EXIT_FAILURE); | |
7a5ca864 FB |
661 | } |
662 | break; | |
8c116b0e WX |
663 | case 'l': |
664 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
70b94331 MA |
665 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
666 | optarg, false); | |
8c116b0e | 667 | if (!sn_opts) { |
85b01e09 MA |
668 | error_report("Failed in parsing snapshot param `%s'", |
669 | optarg); | |
670 | exit(EXIT_FAILURE); | |
8c116b0e WX |
671 | } |
672 | } else { | |
673 | sn_id_or_name = optarg; | |
674 | } | |
675 | /* fall through */ | |
7a5ca864 | 676 | case 'r': |
b90fb4b8 | 677 | nbdflags |= NBD_FLAG_READ_ONLY; |
07108b29 | 678 | flags &= ~BDRV_O_RDWR; |
7a5ca864 FB |
679 | break; |
680 | case 'P': | |
681 | partition = strtol(optarg, &end, 0); | |
713cc671 | 682 | if (*end) { |
85b01e09 MA |
683 | error_report("Invalid partition `%s'", optarg); |
684 | exit(EXIT_FAILURE); | |
713cc671 PL |
685 | } |
686 | if (partition < 1 || partition > 8) { | |
85b01e09 MA |
687 | error_report("Invalid partition %d", partition); |
688 | exit(EXIT_FAILURE); | |
713cc671 | 689 | } |
7a5ca864 | 690 | break; |
cd831bd7 | 691 | case 'k': |
b32f6c28 | 692 | sockpath = optarg; |
713cc671 | 693 | if (sockpath[0] != '/') { |
9af9e0fe | 694 | error_report("socket path must be absolute"); |
85b01e09 | 695 | exit(EXIT_FAILURE); |
713cc671 | 696 | } |
cd831bd7 TS |
697 | break; |
698 | case 'd': | |
699 | disconnect = true; | |
700 | break; | |
701 | case 'c': | |
702 | device = optarg; | |
703 | break; | |
3b05a8e9 TS |
704 | case 'e': |
705 | shared = strtol(optarg, &end, 0); | |
706 | if (*end) { | |
85b01e09 MA |
707 | error_report("Invalid shared device number '%s'", optarg); |
708 | exit(EXIT_FAILURE); | |
3b05a8e9 TS |
709 | } |
710 | if (shared < 1) { | |
9af9e0fe | 711 | error_report("Shared device number must be greater than 0"); |
85b01e09 | 712 | exit(EXIT_FAILURE); |
3b05a8e9 TS |
713 | } |
714 | break; | |
e6b63677 DB |
715 | case 'f': |
716 | fmt = optarg; | |
717 | break; | |
713cc671 PL |
718 | case 't': |
719 | persistent = 1; | |
720 | break; | |
3d4b2f9c DB |
721 | case 'x': |
722 | export_name = optarg; | |
723 | break; | |
b1a75b33 EB |
724 | case 'D': |
725 | export_description = optarg; | |
726 | break; | |
7a5ca864 FB |
727 | case 'v': |
728 | verbose = 1; | |
729 | break; | |
730 | case 'V': | |
731 | version(argv[0]); | |
732 | exit(0); | |
733 | break; | |
734 | case 'h': | |
735 | usage(argv[0]); | |
736 | exit(0); | |
737 | break; | |
738 | case '?': | |
85b01e09 MA |
739 | error_report("Try `%s --help' for more information.", argv[0]); |
740 | exit(EXIT_FAILURE); | |
0ab3b337 DB |
741 | case QEMU_NBD_OPT_OBJECT: { |
742 | QemuOpts *opts; | |
743 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
744 | optarg, true); | |
745 | if (!opts) { | |
746 | exit(EXIT_FAILURE); | |
747 | } | |
748 | } break; | |
145614a1 DB |
749 | case QEMU_NBD_OPT_TLSCREDS: |
750 | tlscredsid = optarg; | |
751 | break; | |
77c9aaef DB |
752 | case QEMU_NBD_OPT_IMAGE_OPTS: |
753 | imageOpts = true; | |
754 | break; | |
39ca463e DL |
755 | case 'T': |
756 | g_free(trace_file); | |
757 | trace_file = trace_opt_parse(optarg); | |
758 | break; | |
ffb31e1d HR |
759 | case QEMU_NBD_OPT_FORK: |
760 | fork_process = true; | |
761 | break; | |
7a5ca864 FB |
762 | } |
763 | } | |
764 | ||
765 | if ((argc - optind) != 1) { | |
433672b0 MA |
766 | error_report("Invalid number of arguments"); |
767 | error_printf("Try `%s --help' for more information.\n", argv[0]); | |
85b01e09 | 768 | exit(EXIT_FAILURE); |
7a5ca864 FB |
769 | } |
770 | ||
0ab3b337 DB |
771 | if (qemu_opts_foreach(&qemu_object_opts, |
772 | user_creatable_add_opts_foreach, | |
51b9b478 | 773 | NULL, NULL)) { |
0ab3b337 DB |
774 | exit(EXIT_FAILURE); |
775 | } | |
776 | ||
39ca463e DL |
777 | if (!trace_init_backends()) { |
778 | exit(1); | |
779 | } | |
780 | trace_init_file(trace_file); | |
781 | qemu_set_log(LOG_TRACE); | |
782 | ||
a721f53b RJ |
783 | socket_activation = check_socket_activation(); |
784 | if (socket_activation == 0) { | |
785 | setup_address_and_port(&bindto, &port); | |
786 | } else { | |
787 | /* Using socket activation - check user didn't use -p etc. */ | |
788 | const char *err_msg = socket_activation_validate_opts(device, sockpath, | |
789 | bindto, port); | |
790 | if (err_msg != NULL) { | |
791 | error_report("%s", err_msg); | |
792 | exit(EXIT_FAILURE); | |
793 | } | |
53fabd4b PB |
794 | |
795 | /* qemu-nbd can only listen on a single socket. */ | |
796 | if (socket_activation > 1) { | |
797 | error_report("qemu-nbd does not support socket activation with %s > 1", | |
798 | "LISTEN_FDS"); | |
799 | exit(EXIT_FAILURE); | |
800 | } | |
a721f53b RJ |
801 | } |
802 | ||
145614a1 DB |
803 | if (tlscredsid) { |
804 | if (sockpath) { | |
805 | error_report("TLS is only supported with IPv4/IPv6"); | |
806 | exit(EXIT_FAILURE); | |
807 | } | |
808 | if (device) { | |
809 | error_report("TLS is not supported with a host device"); | |
810 | exit(EXIT_FAILURE); | |
811 | } | |
812 | if (!export_name) { | |
813 | /* Set the default NBD protocol export name, since | |
814 | * we *must* use new style protocol for TLS */ | |
815 | export_name = ""; | |
816 | } | |
817 | tlscreds = nbd_get_tls_creds(tlscredsid, &local_err); | |
818 | if (local_err) { | |
819 | error_report("Failed to get TLS creds %s", | |
820 | error_get_pretty(local_err)); | |
821 | exit(EXIT_FAILURE); | |
822 | } | |
823 | } | |
824 | ||
cd831bd7 | 825 | if (disconnect) { |
d0d6ff58 DB |
826 | int nbdfd = open(argv[optind], O_RDWR); |
827 | if (nbdfd < 0) { | |
85b01e09 MA |
828 | error_report("Cannot open %s: %s", argv[optind], |
829 | strerror(errno)); | |
830 | exit(EXIT_FAILURE); | |
fc19f8a0 | 831 | } |
d0d6ff58 | 832 | nbd_disconnect(nbdfd); |
cd831bd7 | 833 | |
d0d6ff58 | 834 | close(nbdfd); |
cd831bd7 TS |
835 | |
836 | printf("%s disconnected\n", argv[optind]); | |
837 | ||
713cc671 | 838 | return 0; |
cd831bd7 TS |
839 | } |
840 | ||
ffb31e1d | 841 | if ((device && !verbose) || fork_process) { |
c1f8fdc3 PB |
842 | int stderr_fd[2]; |
843 | pid_t pid; | |
844 | int ret; | |
845 | ||
fc19f8a0 | 846 | if (qemu_pipe(stderr_fd) < 0) { |
85b01e09 MA |
847 | error_report("Error setting up communication pipe: %s", |
848 | strerror(errno)); | |
849 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
850 | } |
851 | ||
852 | /* Now daemonize, but keep a communication channel open to | |
853 | * print errors and exit with the proper status code. | |
854 | */ | |
855 | pid = fork(); | |
70d4739e | 856 | if (pid < 0) { |
85b01e09 MA |
857 | error_report("Failed to fork: %s", strerror(errno)); |
858 | exit(EXIT_FAILURE); | |
70d4739e | 859 | } else if (pid == 0) { |
c1f8fdc3 | 860 | close(stderr_fd[0]); |
9faf31b6 | 861 | ret = qemu_daemon(1, 0); |
c1f8fdc3 PB |
862 | |
863 | /* Temporarily redirect stderr to the parent's pipe... */ | |
ffb31e1d | 864 | old_stderr = dup(STDERR_FILENO); |
c1f8fdc3 | 865 | dup2(stderr_fd[1], STDERR_FILENO); |
fc19f8a0 | 866 | if (ret < 0) { |
85b01e09 MA |
867 | error_report("Failed to daemonize: %s", strerror(errno)); |
868 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
869 | } |
870 | ||
871 | /* ... close the descriptor we inherited and go on. */ | |
872 | close(stderr_fd[1]); | |
873 | } else { | |
874 | bool errors = false; | |
875 | char *buf; | |
876 | ||
877 | /* In the parent. Print error messages from the child until | |
878 | * it closes the pipe. | |
879 | */ | |
880 | close(stderr_fd[1]); | |
881 | buf = g_malloc(1024); | |
882 | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { | |
883 | errors = true; | |
884 | ret = qemu_write_full(STDERR_FILENO, buf, ret); | |
fc19f8a0 | 885 | if (ret < 0) { |
c1f8fdc3 PB |
886 | exit(EXIT_FAILURE); |
887 | } | |
888 | } | |
fc19f8a0 | 889 | if (ret < 0) { |
85b01e09 MA |
890 | error_report("Cannot read from daemon: %s", |
891 | strerror(errno)); | |
892 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
893 | } |
894 | ||
895 | /* Usually the daemon should not print any message. | |
896 | * Exit with zero status in that case. | |
897 | */ | |
898 | exit(errors); | |
899 | } | |
900 | } | |
901 | ||
a6ac2313 PB |
902 | if (device != NULL && sockpath == NULL) { |
903 | sockpath = g_malloc(128); | |
904 | snprintf(sockpath, 128, SOCKET_PATH, basename(device)); | |
cd831bd7 TS |
905 | } |
906 | ||
e4849c1d | 907 | server = qio_net_listener_new(); |
a721f53b | 908 | if (socket_activation == 0) { |
a721f53b | 909 | saddr = nbd_build_socket_address(sockpath, bindto, port); |
e4849c1d DB |
910 | if (qio_net_listener_open_sync(server, saddr, &local_err) < 0) { |
911 | object_unref(OBJECT(server)); | |
a721f53b | 912 | error_report_err(local_err); |
e4849c1d | 913 | exit(EXIT_FAILURE); |
a721f53b RJ |
914 | } |
915 | } else { | |
e4849c1d | 916 | size_t i; |
a721f53b | 917 | /* See comment in check_socket_activation above. */ |
e4849c1d DB |
918 | for (i = 0; i < socket_activation; i++) { |
919 | QIOChannelSocket *sioc; | |
920 | sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i, | |
921 | &local_err); | |
922 | if (sioc == NULL) { | |
923 | object_unref(OBJECT(server)); | |
924 | error_report("Failed to use socket activation: %s", | |
925 | error_get_pretty(local_err)); | |
926 | exit(EXIT_FAILURE); | |
927 | } | |
928 | qio_net_listener_add(server, sioc); | |
929 | object_unref(OBJECT(sioc)); | |
a721f53b RJ |
930 | } |
931 | } | |
48bec07e | 932 | |
2f78e491 | 933 | if (qemu_init_main_loop(&local_err)) { |
565f65d2 | 934 | error_report_err(local_err); |
2f78e491 CN |
935 | exit(EXIT_FAILURE); |
936 | } | |
802ddc37 | 937 | bdrv_init(); |
b3b5299d | 938 | atexit(qemu_nbd_shutdown); |
802ddc37 | 939 | |
77c9aaef DB |
940 | srcpath = argv[optind]; |
941 | if (imageOpts) { | |
942 | QemuOpts *opts; | |
943 | if (fmt) { | |
944 | error_report("--image-opts and -f are mutually exclusive"); | |
945 | exit(EXIT_FAILURE); | |
946 | } | |
947 | opts = qemu_opts_parse_noisily(&file_opts, srcpath, true); | |
948 | if (!opts) { | |
949 | qemu_opts_reset(&file_opts); | |
950 | exit(EXIT_FAILURE); | |
951 | } | |
952 | options = qemu_opts_to_qdict(opts, NULL); | |
953 | qemu_opts_reset(&file_opts); | |
efaa7c4e | 954 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
77c9aaef DB |
955 | } else { |
956 | if (fmt) { | |
957 | options = qdict_new(); | |
46f5ac20 | 958 | qdict_put_str(options, "driver", fmt); |
77c9aaef | 959 | } |
efaa7c4e | 960 | blk = blk_new_open(srcpath, NULL, options, flags, &local_err); |
e6b63677 DB |
961 | } |
962 | ||
4fbec260 | 963 | if (!blk) { |
c29b77f9 MA |
964 | error_reportf_err(local_err, "Failed to blk_new_open '%s': ", |
965 | argv[optind]); | |
85b01e09 | 966 | exit(EXIT_FAILURE); |
802ddc37 | 967 | } |
4fbec260 | 968 | bs = blk_bs(blk); |
802ddc37 | 969 | |
6effd5bf KW |
970 | blk_set_enable_write_cache(blk, !writethrough); |
971 | ||
8c116b0e WX |
972 | if (sn_opts) { |
973 | ret = bdrv_snapshot_load_tmp(bs, | |
974 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
975 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
976 | &local_err); | |
977 | } else if (sn_id_or_name) { | |
978 | ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name, | |
979 | &local_err); | |
980 | } | |
981 | if (ret < 0) { | |
c29b77f9 | 982 | error_reportf_err(local_err, "Failed to load snapshot: "); |
85b01e09 | 983 | exit(EXIT_FAILURE); |
8c116b0e WX |
984 | } |
985 | ||
b3838a40 | 986 | bs->detect_zeroes = detect_zeroes; |
4c58e80a | 987 | fd_size = blk_getlength(blk); |
98f44bbe | 988 | if (fd_size < 0) { |
85b01e09 MA |
989 | error_report("Failed to determine the image length: %s", |
990 | strerror(-fd_size)); | |
991 | exit(EXIT_FAILURE); | |
98f44bbe | 992 | } |
802ddc37 | 993 | |
e424b655 TG |
994 | if (dev_offset >= fd_size) { |
995 | error_report("Offset (%lld) has to be smaller than the image size " | |
996 | "(%lld)", | |
997 | (long long int)dev_offset, (long long int)fd_size); | |
998 | exit(EXIT_FAILURE); | |
999 | } | |
1000 | fd_size -= dev_offset; | |
1001 | ||
185b4338 | 1002 | if (partition != -1) { |
4c58e80a | 1003 | ret = find_partition(blk, partition, &dev_offset, &fd_size); |
185b4338 | 1004 | if (ret < 0) { |
85b01e09 | 1005 | error_report("Could not find partition %d: %s", partition, |
a4699e55 | 1006 | strerror(-ret)); |
85b01e09 | 1007 | exit(EXIT_FAILURE); |
185b4338 | 1008 | } |
802ddc37 PB |
1009 | } |
1010 | ||
cd7fca95 KW |
1011 | exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed, |
1012 | writethrough, NULL, &local_err); | |
98f44bbe | 1013 | if (!exp) { |
4fffeb5e | 1014 | error_report_err(local_err); |
85b01e09 | 1015 | exit(EXIT_FAILURE); |
98f44bbe | 1016 | } |
3d4b2f9c DB |
1017 | if (export_name) { |
1018 | nbd_export_set_name(exp, export_name); | |
b1a75b33 | 1019 | nbd_export_set_description(exp, export_description); |
3d4b2f9c | 1020 | newproto = true; |
b1a75b33 EB |
1021 | } else if (export_description) { |
1022 | error_report("Export description requires an export name"); | |
1023 | exit(EXIT_FAILURE); | |
3d4b2f9c | 1024 | } |
3b05a8e9 | 1025 | |
f1ef5555 PB |
1026 | if (device) { |
1027 | int ret; | |
1028 | ||
a6ac2313 | 1029 | ret = pthread_create(&client_thread, NULL, nbd_client_thread, device); |
f1ef5555 | 1030 | if (ret != 0) { |
85b01e09 MA |
1031 | error_report("Failed to create client thread: %s", strerror(ret)); |
1032 | exit(EXIT_FAILURE); | |
f1ef5555 PB |
1033 | } |
1034 | } else { | |
1035 | /* Shut up GCC warnings. */ | |
1036 | memset(&client_thread, 0, sizeof(client_thread)); | |
1037 | } | |
1038 | ||
d0d6ff58 | 1039 | nbd_update_server_watch(); |
7a5ca864 | 1040 | |
9faf31b6 MT |
1041 | /* now when the initialization is (almost) complete, chdir("/") |
1042 | * to free any busy filesystems */ | |
1043 | if (chdir("/") < 0) { | |
85b01e09 MA |
1044 | error_report("Could not chdir to root directory: %s", |
1045 | strerror(errno)); | |
1046 | exit(EXIT_FAILURE); | |
9faf31b6 MT |
1047 | } |
1048 | ||
ffb31e1d HR |
1049 | if (fork_process) { |
1050 | dup2(old_stderr, STDERR_FILENO); | |
1051 | close(old_stderr); | |
1052 | } | |
1053 | ||
7860a380 | 1054 | state = RUNNING; |
3b05a8e9 | 1055 | do { |
a61c6782 | 1056 | main_loop_wait(false); |
7860a380 PB |
1057 | if (state == TERMINATE) { |
1058 | state = TERMINATING; | |
1059 | nbd_export_close(exp); | |
1060 | nbd_export_put(exp); | |
1061 | exp = NULL; | |
1062 | } | |
1063 | } while (state != TERMINATED); | |
7a5ca864 | 1064 | |
26f54e9a | 1065 | blk_unref(blk); |
b32f6c28 PB |
1066 | if (sockpath) { |
1067 | unlink(sockpath); | |
1068 | } | |
7a5ca864 | 1069 | |
fbf28a43 | 1070 | qemu_opts_del(sn_opts); |
8c116b0e | 1071 | |
a517e88b PB |
1072 | if (device) { |
1073 | void *ret; | |
1074 | pthread_join(client_thread, &ret); | |
1075 | exit(ret != NULL); | |
1076 | } else { | |
1077 | exit(EXIT_SUCCESS); | |
1078 | } | |
7a5ca864 | 1079 | } |