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