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