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