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