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