ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 1 | /* |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
Eric Blake | c2a3d7d | 2017-07-21 08:50:47 -0500 | [diff] [blame] | 20 | #include <getopt.h> |
| 21 | #include <libgen.h> |
| 22 | #include <pthread.h> |
| 23 | |
Marc-André Lureau | 49f9522 | 2022-04-20 17:25:49 +0400 | [diff] [blame] | 24 | #include "qemu/help-texts.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 25 | #include "qapi/error.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 26 | #include "qemu/cutils.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 27 | #include "sysemu/block-backend.h" |
Stefan Hajnoczi | cbc20bf | 2020-09-29 13:55:15 +0100 | [diff] [blame] | 28 | #include "sysemu/runstate.h" /* for qemu_system_killed() prototype */ |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 29 | #include "block/block_int.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 30 | #include "block/nbd.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 31 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 32 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 33 | #include "qemu/option.h" |
Paolo Bonzini | 537b41f | 2014-02-17 14:43:51 +0100 | [diff] [blame] | 34 | #include "qemu/error-report.h" |
Daniel P. Berrange | 0ab3b33 | 2016-02-10 18:41:00 +0000 | [diff] [blame] | 35 | #include "qemu/config-file.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 36 | #include "qemu/bswap.h" |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 37 | #include "qemu/log.h" |
Paolo Bonzini | 53fabd4 | 2017-03-16 16:29:45 +0100 | [diff] [blame] | 38 | #include "qemu/systemd.h" |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 39 | #include "block/snapshot.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 40 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 41 | #include "qapi/qmp/qstring.h" |
Daniel P. Berrange | 0ab3b33 | 2016-02-10 18:41:00 +0000 | [diff] [blame] | 42 | #include "qom/object_interfaces.h" |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 43 | #include "io/channel-socket.h" |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 44 | #include "io/net-listener.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 45 | #include "crypto/init.h" |
Philippe Mathieu-Daudé | 0279cd9 | 2021-06-28 18:09:10 +0200 | [diff] [blame] | 46 | #include "crypto/tlscreds.h" |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 47 | #include "trace/control.h" |
Eric Blake | be37713 | 2017-07-21 08:50:46 -0500 | [diff] [blame] | 48 | #include "qemu-version.h" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 49 | |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 50 | #ifdef CONFIG_SELINUX |
| 51 | #include <selinux/selinux.h> |
| 52 | #endif |
| 53 | |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 54 | #ifdef __linux__ |
| 55 | #define HAVE_NBD_DEVICE 1 |
| 56 | #else |
| 57 | #define HAVE_NBD_DEVICE 0 |
| 58 | #endif |
| 59 | |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 60 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
Daniel P. Berrange | fa8b7ce | 2016-02-17 10:10:21 +0000 | [diff] [blame] | 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 |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 68 | #define QEMU_NBD_OPT_FORK 263 |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 69 | #define QEMU_NBD_OPT_TLSAUTHZ 264 |
Max Reitz | 637bc5a | 2019-05-08 23:18:16 +0200 | [diff] [blame] | 70 | #define QEMU_NBD_OPT_PID_FILE 265 |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 71 | #define QEMU_NBD_OPT_SELINUX_LABEL 266 |
Daniel P. Berrangé | 003b2b2 | 2022-03-04 19:36:02 +0000 | [diff] [blame] | 72 | #define QEMU_NBD_OPT_TLSHOSTNAME 267 |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 73 | |
Eric Blake | bd31c21 | 2016-05-06 10:26:42 -0600 | [diff] [blame] | 74 | #define MBR_SIZE 512 |
| 75 | |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 76 | static int verbose; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 77 | static char *srcpath; |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 78 | static SocketAddress *saddr; |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 79 | static int persistent = 0; |
Kevin Wolf | d794f7f | 2020-09-24 17:26:56 +0200 | [diff] [blame] | 80 | static enum { RUNNING, TERMINATE, TERMINATED } state; |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 81 | static int shared = 1; |
| 82 | static int nb_fds; |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 83 | static QIONetListener *server; |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 84 | static QCryptoTLSCreds *tlscreds; |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 85 | static const char *tlsauthz; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 86 | |
| 87 | static void usage(const char *name) |
| 88 | { |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 89 | (printf) ( |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 90 | "Usage: %s [OPTIONS] FILE\n" |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 91 | " or: %s -L [OPTIONS]\n" |
| 92 | "QEMU Disk Network Block Device Utility\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 93 | "\n" |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 94 | " -h, --help display this help and exit\n" |
| 95 | " -V, --version output version information and exit\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 96 | "\n" |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 97 | "Connection properties:\n" |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 98 | " -p, --port=PORT port to listen on (default `%d')\n" |
| 99 | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" |
| 100 | " -k, --socket=PATH path to the unix socket\n" |
| 101 | " (default '"SOCKET_PATH"')\n" |
| 102 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" |
| 103 | " -t, --persistent don't exit on the last connection\n" |
| 104 | " -v, --verbose display extra debugging information\n" |
Vladimir Sementsov-Ogievskiy | f5cd0bb | 2018-10-03 20:02:27 +0300 | [diff] [blame] | 105 | " -x, --export-name=NAME expose export by name (default is empty string)\n" |
| 106 | " -D, --description=TEXT export a human-readable description\n" |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 107 | "\n" |
| 108 | "Exposing part of the image:\n" |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 109 | " -o, --offset=OFFSET offset into the image\n" |
Eric Blake | dbc7b01 | 2020-10-27 00:05:55 -0500 | [diff] [blame] | 110 | " -A, --allocation-depth expose the allocation depth\n" |
Eric Blake | 636192c | 2019-01-11 13:47:20 -0600 | [diff] [blame] | 111 | " -B, --bitmap=NAME expose a persistent dirty bitmap\n" |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 112 | "\n" |
Daniel P. Berrange | 0ab3b33 | 2016-02-10 18:41:00 +0000 | [diff] [blame] | 113 | "General purpose options:\n" |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 114 | " -L, --list list exports available from another NBD server\n" |
Daniel P. Berrange | 0ab3b33 | 2016-02-10 18:41:00 +0000 | [diff] [blame] | 115 | " --object type,id=ID,... define an object such as 'secret' for providing\n" |
| 116 | " passwords and/or encryption keys\n" |
Eric Blake | f7812df | 2018-10-03 13:04:26 -0500 | [diff] [blame] | 117 | " --tls-creds=ID use id of an earlier --object to provide TLS\n" |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 118 | " --tls-authz=ID use id of an earlier --object to provide\n" |
| 119 | " authorization\n" |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 120 | " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 121 | " specify tracing options\n" |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 122 | " --fork fork off the server process and exit the parent\n" |
| 123 | " once the server is running\n" |
Max Reitz | 637bc5a | 2019-05-08 23:18:16 +0200 | [diff] [blame] | 124 | " --pid-file=PATH store the server's process ID in the given file\n" |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 125 | #ifdef CONFIG_SELINUX |
| 126 | " --selinux-label=LABEL set SELinux process label on listening socket\n" |
| 127 | #endif |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 128 | #if HAVE_NBD_DEVICE |
| 129 | "\n" |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 130 | "Kernel NBD client support:\n" |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 131 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
| 132 | " -d, --disconnect disconnect the specified device\n" |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 133 | #endif |
| 134 | "\n" |
| 135 | "Block device options:\n" |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 136 | " -f, --format=FORMAT set image format (raw, qcow2, ...)\n" |
| 137 | " -r, --read-only export read-only\n" |
| 138 | " -s, --snapshot use FILE as an external snapshot, create a temporary\n" |
| 139 | " file with backing_file=FILE, redirect the write to\n" |
| 140 | " the temporary one\n" |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 141 | " -l, --load-snapshot=SNAPSHOT_PARAM\n" |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 142 | " load an internal snapshot inside FILE and export it\n" |
| 143 | " as an read-only device, SNAPSHOT_PARAM format is\n" |
| 144 | " 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 145 | " '[ID_OR_NAME]'\n" |
| 146 | " -n, --nocache disable host cache\n" |
Nir Soffer | 0961525 | 2021-08-13 23:55:19 +0300 | [diff] [blame] | 147 | " --cache=MODE set cache mode used to access the disk image, the\n" |
| 148 | " valid options are: 'none', 'writeback' (default),\n" |
| 149 | " 'writethrough', 'directsync' and 'unsafe'\n" |
Aarushi Mehta | 7680274 | 2020-01-20 14:18:56 +0000 | [diff] [blame] | 150 | " --aio=MODE set AIO mode (native, io_uring or threads)\n" |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 151 | " --discard=MODE set discard mode (ignore, unmap)\n" |
Andrey Korolyov | 6883de6 | 2015-07-31 22:12:54 +0300 | [diff] [blame] | 152 | " --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n" |
Daniel P. Berrange | 77c9aae | 2016-02-17 10:10:19 +0000 | [diff] [blame] | 153 | " --image-opts treat FILE as a full set of image options\n" |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 154 | "\n" |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 155 | QEMU_HELP_BOTTOM "\n" |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 156 | , name, name, NBD_DEFAULT_PORT, "DEVICE"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void version(const char *name) |
| 160 | { |
| 161 | printf( |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 162 | "%s " QEMU_FULL_VERSION "\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 163 | "Written by Anthony Liguori.\n" |
| 164 | "\n" |
Eric Blake | be37713 | 2017-07-21 08:50:46 -0500 | [diff] [blame] | 165 | QEMU_COPYRIGHT "\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 166 | "This is free software; see the source for copying conditions. There is NO\n" |
| 167 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" |
ths | 315bc7a | 2008-07-18 18:06:23 +0000 | [diff] [blame] | 168 | , name); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Eric Blake | 029a88c | 2020-09-30 07:11:01 -0500 | [diff] [blame] | 171 | #ifdef CONFIG_POSIX |
Stefan Hajnoczi | cbc20bf | 2020-09-29 13:55:15 +0100 | [diff] [blame] | 172 | /* |
| 173 | * The client thread uses SIGTERM to interrupt the server. A signal |
| 174 | * handler ensures that "qemu-nbd -v -c" exits with a nice status code. |
| 175 | */ |
| 176 | void qemu_system_killed(int signum, pid_t pid) |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 177 | { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 178 | qatomic_cmpxchg(&state, RUNNING, TERMINATE); |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 179 | qemu_notify_event(); |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 180 | } |
Eric Blake | 029a88c | 2020-09-30 07:11:01 -0500 | [diff] [blame] | 181 | #endif /* CONFIG_POSIX */ |
Paolo Bonzini | 537b41f | 2014-02-17 14:43:51 +0100 | [diff] [blame] | 182 | |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 183 | static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls, |
| 184 | const char *hostname) |
| 185 | { |
| 186 | int ret = EXIT_FAILURE; |
| 187 | int rc; |
| 188 | Error *err = NULL; |
| 189 | QIOChannelSocket *sioc; |
| 190 | NBDExportInfo *list; |
| 191 | int i, j; |
| 192 | |
| 193 | sioc = qio_channel_socket_new(); |
| 194 | if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) { |
| 195 | error_report_err(err); |
Alex Chen | 992809b | 2020-11-30 12:36:51 +0000 | [diff] [blame] | 196 | goto out; |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 197 | } |
| 198 | rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list, |
| 199 | &err); |
| 200 | if (rc < 0) { |
| 201 | if (err) { |
| 202 | error_report_err(err); |
| 203 | } |
| 204 | goto out; |
| 205 | } |
| 206 | printf("exports available: %d\n", rc); |
| 207 | for (i = 0; i < rc; i++) { |
| 208 | printf(" export: '%s'\n", list[i].name); |
| 209 | if (list[i].description && *list[i].description) { |
| 210 | printf(" description: %s\n", list[i].description); |
| 211 | } |
| 212 | if (list[i].flags & NBD_FLAG_HAS_FLAGS) { |
Max Reitz | c4e2aff | 2019-04-05 21:16:35 +0200 | [diff] [blame] | 213 | static const char *const flag_names[] = { |
| 214 | [NBD_FLAG_READ_ONLY_BIT] = "readonly", |
| 215 | [NBD_FLAG_SEND_FLUSH_BIT] = "flush", |
| 216 | [NBD_FLAG_SEND_FUA_BIT] = "fua", |
| 217 | [NBD_FLAG_ROTATIONAL_BIT] = "rotational", |
| 218 | [NBD_FLAG_SEND_TRIM_BIT] = "trim", |
| 219 | [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes", |
| 220 | [NBD_FLAG_SEND_DF_BIT] = "df", |
| 221 | [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi", |
| 222 | [NBD_FLAG_SEND_RESIZE_BIT] = "resize", |
| 223 | [NBD_FLAG_SEND_CACHE_BIT] = "cache", |
Eric Blake | 0a47954 | 2019-08-23 09:37:23 -0500 | [diff] [blame] | 224 | [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero", |
Max Reitz | c4e2aff | 2019-04-05 21:16:35 +0200 | [diff] [blame] | 225 | }; |
| 226 | |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 227 | printf(" size: %" PRIu64 "\n", list[i].size); |
| 228 | printf(" flags: 0x%x (", list[i].flags); |
Max Reitz | c4e2aff | 2019-04-05 21:16:35 +0200 | [diff] [blame] | 229 | for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) { |
| 230 | if (flag_names[bit] && (list[i].flags & (1 << bit))) { |
| 231 | printf(" %s", flag_names[bit]); |
| 232 | } |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 233 | } |
| 234 | printf(" )\n"); |
| 235 | } |
| 236 | if (list[i].min_block) { |
| 237 | printf(" min block: %u\n", list[i].min_block); |
| 238 | printf(" opt block: %u\n", list[i].opt_block); |
| 239 | printf(" max block: %u\n", list[i].max_block); |
| 240 | } |
| 241 | if (list[i].n_contexts) { |
| 242 | printf(" available meta contexts: %d\n", list[i].n_contexts); |
| 243 | for (j = 0; j < list[i].n_contexts; j++) { |
| 244 | printf(" %s\n", list[i].contexts[j]); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | nbd_free_export_list(list, rc); |
| 249 | |
| 250 | ret = EXIT_SUCCESS; |
| 251 | out: |
| 252 | object_unref(OBJECT(sioc)); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 257 | #if HAVE_NBD_DEVICE |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 258 | static void *show_parts(void *arg) |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 259 | { |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 260 | char *device = arg; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 261 | int nbd; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 262 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 263 | /* linux just needs an open() to trigger |
| 264 | * the partition table update |
| 265 | * but remember to load the module with max_part != 0 : |
| 266 | * modprobe nbd max_part=63 |
| 267 | */ |
| 268 | nbd = open(device, O_RDWR); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 269 | if (nbd >= 0) { |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 270 | close(nbd); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 271 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | static void *nbd_client_thread(void *arg) |
| 276 | { |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 277 | char *device = arg; |
Eric Blake | 6dc1667 | 2019-01-17 13:36:46 -0600 | [diff] [blame] | 278 | NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") }; |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 279 | QIOChannelSocket *sioc; |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 280 | int fd = -1; |
| 281 | int ret = EXIT_FAILURE; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 282 | pthread_t show_parts_thread; |
Max Reitz | 1ce5284 | 2015-01-26 21:02:59 -0500 | [diff] [blame] | 283 | Error *local_error = NULL; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 284 | |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 285 | sioc = qio_channel_socket_new(); |
| 286 | if (qio_channel_socket_connect_sync(sioc, |
| 287 | saddr, |
| 288 | &local_error) < 0) { |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 289 | error_report_err(local_error); |
Stefan Hajnoczi | dc10e8b | 2012-01-05 13:16:07 +0000 | [diff] [blame] | 290 | goto out; |
| 291 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 292 | |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 293 | if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), |
| 294 | NULL, NULL, NULL, &info, &local_error) < 0) { |
Max Reitz | 1ce5284 | 2015-01-26 21:02:59 -0500 | [diff] [blame] | 295 | if (local_error) { |
Markus Armbruster | 7828867 | 2015-12-18 16:35:07 +0100 | [diff] [blame] | 296 | error_report_err(local_error); |
Max Reitz | 1ce5284 | 2015-01-26 21:02:59 -0500 | [diff] [blame] | 297 | } |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 298 | goto out; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 301 | fd = open(device, O_RDWR); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 302 | if (fd < 0) { |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 303 | /* Linux-only, we can use %m in printf. */ |
Markus Armbruster | b988468 | 2015-12-18 16:35:18 +0100 | [diff] [blame] | 304 | error_report("Failed to open %s: %m", device); |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 305 | goto out; |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 308 | if (nbd_init(fd, sioc, &info, &local_error) < 0) { |
Vladimir Sementsov-Ogievskiy | be41c10 | 2017-05-26 14:09:13 +0300 | [diff] [blame] | 309 | error_report_err(local_error); |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 310 | goto out; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* update partition table */ |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 314 | pthread_create(&show_parts_thread, NULL, show_parts, device); |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 315 | |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 316 | if (verbose) { |
| 317 | fprintf(stderr, "NBD device %s is now connected to %s\n", |
| 318 | device, srcpath); |
| 319 | } else { |
| 320 | /* Close stderr so that the qemu-nbd process exits. */ |
| 321 | dup2(STDOUT_FILENO, STDERR_FILENO); |
| 322 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 323 | |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 324 | if (nbd_client(fd) < 0) { |
| 325 | goto out; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 326 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 327 | |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 328 | ret = EXIT_SUCCESS; |
| 329 | |
| 330 | out: |
| 331 | if (fd >= 0) { |
| 332 | close(fd); |
| 333 | } |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 334 | object_unref(OBJECT(sioc)); |
Eric Blake | 6dc1667 | 2019-01-17 13:36:46 -0600 | [diff] [blame] | 335 | g_free(info.name); |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 336 | kill(getpid(), SIGTERM); |
Alex Chen | af74b55 | 2020-12-08 13:49:44 +0000 | [diff] [blame] | 337 | return (void *) (intptr_t) ret; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 338 | } |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 339 | #endif /* HAVE_NBD_DEVICE */ |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 340 | |
Fam Zheng | e4afbf4 | 2015-05-19 10:50:59 +0000 | [diff] [blame] | 341 | static int nbd_can_accept(void) |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 342 | { |
Eric Blake | 3dcf56e | 2021-02-09 09:27:59 -0600 | [diff] [blame] | 343 | return state == RUNNING && (shared == 0 || nb_fds < shared); |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 346 | static void nbd_update_server_watch(void); |
Fam Zheng | e4afbf4 | 2015-05-19 10:50:59 +0000 | [diff] [blame] | 347 | |
Eric Blake | 0c9390d | 2017-06-08 17:26:17 -0500 | [diff] [blame] | 348 | static void nbd_client_closed(NBDClient *client, bool negotiated) |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 349 | { |
Paolo Bonzini | 1743b51 | 2011-09-19 14:33:23 +0200 | [diff] [blame] | 350 | nb_fds--; |
Eric Blake | 0c9390d | 2017-06-08 17:26:17 -0500 | [diff] [blame] | 351 | if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) { |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 352 | state = TERMINATE; |
| 353 | } |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 354 | nbd_update_server_watch(); |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 355 | nbd_client_put(client); |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 358 | static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc, |
| 359 | gpointer opaque) |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 360 | { |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 361 | if (state >= TERMINATE) { |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 362 | return; |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Fam Zheng | ee7d7aa | 2016-01-14 16:41:01 +0800 | [diff] [blame] | 365 | nb_fds++; |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 366 | nbd_update_server_watch(); |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 367 | nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed); |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 370 | static void nbd_update_server_watch(void) |
Fam Zheng | e4afbf4 | 2015-05-19 10:50:59 +0000 | [diff] [blame] | 371 | { |
| 372 | if (nbd_can_accept()) { |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 373 | qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL); |
Fam Zheng | e4afbf4 | 2015-05-19 10:50:59 +0000 | [diff] [blame] | 374 | } else { |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 375 | qio_net_listener_set_client_func(server, NULL, NULL, NULL); |
Fam Zheng | e4afbf4 | 2015-05-19 10:50:59 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 379 | |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 380 | static SocketAddress *nbd_build_socket_address(const char *sockpath, |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 381 | const char *bindto, |
| 382 | const char *port) |
| 383 | { |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 384 | SocketAddress *saddr; |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 385 | |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 386 | saddr = g_new0(SocketAddress, 1); |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 387 | if (sockpath) { |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 388 | saddr->type = SOCKET_ADDRESS_TYPE_UNIX; |
| 389 | saddr->u.q_unix.path = g_strdup(sockpath); |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 390 | } else { |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 391 | InetSocketAddress *inet; |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 392 | saddr->type = SOCKET_ADDRESS_TYPE_INET; |
| 393 | inet = &saddr->u.inet; |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 394 | inet->host = g_strdup(bindto); |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 395 | if (port) { |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 396 | inet->port = g_strdup(port); |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 397 | } else { |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 398 | inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | return saddr; |
| 403 | } |
| 404 | |
| 405 | |
Daniel P. Berrange | 77c9aae | 2016-02-17 10:10:19 +0000 | [diff] [blame] | 406 | static QemuOptsList file_opts = { |
| 407 | .name = "file", |
| 408 | .implied_opt_name = "file", |
| 409 | .head = QTAILQ_HEAD_INITIALIZER(file_opts.head), |
| 410 | .desc = { |
| 411 | /* no elements => accept any params */ |
| 412 | { /* end of list */ } |
| 413 | }, |
| 414 | }; |
| 415 | |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 416 | static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list, |
| 417 | Error **errp) |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 418 | { |
| 419 | Object *obj; |
| 420 | QCryptoTLSCreds *creds; |
| 421 | |
| 422 | obj = object_resolve_path_component( |
| 423 | object_get_objects_root(), id); |
| 424 | if (!obj) { |
| 425 | error_setg(errp, "No TLS credentials with id '%s'", |
| 426 | id); |
| 427 | return NULL; |
| 428 | } |
| 429 | creds = (QCryptoTLSCreds *) |
| 430 | object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); |
| 431 | if (!creds) { |
| 432 | error_setg(errp, "Object with id '%s' is not TLS credentials", |
| 433 | id); |
| 434 | return NULL; |
| 435 | } |
| 436 | |
Philippe Mathieu-Daudé | 0279cd9 | 2021-06-28 18:09:10 +0200 | [diff] [blame] | 437 | if (!qcrypto_tls_creds_check_endpoint(creds, |
| 438 | list |
| 439 | ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT |
| 440 | : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, |
| 441 | errp)) { |
| 442 | return NULL; |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 443 | } |
| 444 | object_ref(obj); |
| 445 | return creds; |
| 446 | } |
| 447 | |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 448 | static void setup_address_and_port(const char **address, const char **port) |
| 449 | { |
| 450 | if (*address == NULL) { |
| 451 | *address = "0.0.0.0"; |
| 452 | } |
| 453 | |
| 454 | if (*port == NULL) { |
| 455 | *port = stringify(NBD_DEFAULT_PORT); |
| 456 | } |
| 457 | } |
| 458 | |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 459 | /* |
| 460 | * Check socket parameters compatibility when socket activation is used. |
| 461 | */ |
| 462 | static const char *socket_activation_validate_opts(const char *device, |
| 463 | const char *sockpath, |
| 464 | const char *address, |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 465 | const char *port, |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 466 | const char *selinux, |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 467 | bool list) |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 468 | { |
| 469 | if (device != NULL) { |
| 470 | return "NBD device can't be set when using socket activation"; |
| 471 | } |
| 472 | |
| 473 | if (sockpath != NULL) { |
| 474 | return "Unix socket can't be set when using socket activation"; |
| 475 | } |
| 476 | |
| 477 | if (address != NULL) { |
| 478 | return "The interface can't be set when using socket activation"; |
| 479 | } |
| 480 | |
| 481 | if (port != NULL) { |
| 482 | return "TCP port number can't be set when using socket activation"; |
| 483 | } |
| 484 | |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 485 | if (selinux != NULL) { |
| 486 | return "SELinux label can't be set when using socket activation"; |
| 487 | } |
| 488 | |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 489 | if (list) { |
| 490 | return "List mode is incompatible with socket activation"; |
| 491 | } |
| 492 | |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 493 | return NULL; |
| 494 | } |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 495 | |
Kevin Wolf | b3b5299 | 2018-05-16 13:46:37 +0200 | [diff] [blame] | 496 | static void qemu_nbd_shutdown(void) |
| 497 | { |
| 498 | job_cancel_sync_all(); |
Sergio Lopez | 1895b97 | 2021-02-01 13:50:32 +0100 | [diff] [blame] | 499 | blk_exp_close_all(); |
Kevin Wolf | b3b5299 | 2018-05-16 13:46:37 +0200 | [diff] [blame] | 500 | bdrv_close_all(); |
| 501 | } |
| 502 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 503 | int main(int argc, char **argv) |
| 504 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 505 | BlockBackend *blk; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 506 | BlockDriverState *bs; |
Eric Blake | 9d26dfc | 2019-01-17 13:36:43 -0600 | [diff] [blame] | 507 | uint64_t dev_offset = 0; |
Eric Blake | dbb38ca | 2019-08-23 09:37:22 -0500 | [diff] [blame] | 508 | bool readonly = false; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 509 | bool disconnect = false; |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 510 | const char *bindto = NULL; |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 511 | const char *port = NULL; |
| 512 | char *sockpath = NULL; |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 513 | char *device = NULL; |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 514 | QemuOpts *sn_opts = NULL; |
| 515 | const char *sn_id_or_name = NULL; |
Eric Blake | dbc7b01 | 2020-10-27 00:05:55 -0500 | [diff] [blame] | 516 | const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L"; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 517 | struct option lopt[] = { |
Daniel P. Berrange | aa6e546 | 2016-02-17 10:10:22 +0000 | [diff] [blame] | 518 | { "help", no_argument, NULL, 'h' }, |
| 519 | { "version", no_argument, NULL, 'V' }, |
| 520 | { "bind", required_argument, NULL, 'b' }, |
| 521 | { "port", required_argument, NULL, 'p' }, |
| 522 | { "socket", required_argument, NULL, 'k' }, |
| 523 | { "offset", required_argument, NULL, 'o' }, |
| 524 | { "read-only", no_argument, NULL, 'r' }, |
Eric Blake | dbc7b01 | 2020-10-27 00:05:55 -0500 | [diff] [blame] | 525 | { "allocation-depth", no_argument, NULL, 'A' }, |
Eric Blake | 636192c | 2019-01-11 13:47:20 -0600 | [diff] [blame] | 526 | { "bitmap", required_argument, NULL, 'B' }, |
Daniel P. Berrange | aa6e546 | 2016-02-17 10:10:22 +0000 | [diff] [blame] | 527 | { "connect", required_argument, NULL, 'c' }, |
| 528 | { "disconnect", no_argument, NULL, 'd' }, |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 529 | { "list", no_argument, NULL, 'L' }, |
Daniel P. Berrange | aa6e546 | 2016-02-17 10:10:22 +0000 | [diff] [blame] | 530 | { "snapshot", no_argument, NULL, 's' }, |
| 531 | { "load-snapshot", required_argument, NULL, 'l' }, |
| 532 | { "nocache", no_argument, NULL, 'n' }, |
| 533 | { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE }, |
| 534 | { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO }, |
| 535 | { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD }, |
| 536 | { "detect-zeroes", required_argument, NULL, |
| 537 | QEMU_NBD_OPT_DETECT_ZEROES }, |
| 538 | { "shared", required_argument, NULL, 'e' }, |
| 539 | { "format", required_argument, NULL, 'f' }, |
| 540 | { "persistent", no_argument, NULL, 't' }, |
| 541 | { "verbose", no_argument, NULL, 'v' }, |
| 542 | { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT }, |
| 543 | { "export-name", required_argument, NULL, 'x' }, |
Eric Blake | b1a75b3 | 2016-10-14 13:33:03 -0500 | [diff] [blame] | 544 | { "description", required_argument, NULL, 'D' }, |
Daniel P. Berrange | aa6e546 | 2016-02-17 10:10:22 +0000 | [diff] [blame] | 545 | { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS }, |
Daniel P. Berrangé | 003b2b2 | 2022-03-04 19:36:02 +0000 | [diff] [blame] | 546 | { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME }, |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 547 | { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ }, |
Daniel P. Berrange | aa6e546 | 2016-02-17 10:10:22 +0000 | [diff] [blame] | 548 | { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS }, |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 549 | { "trace", required_argument, NULL, 'T' }, |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 550 | { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK }, |
Max Reitz | 637bc5a | 2019-05-08 23:18:16 +0200 | [diff] [blame] | 551 | { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE }, |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 552 | { "selinux-label", required_argument, NULL, |
| 553 | QEMU_NBD_OPT_SELINUX_LABEL }, |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 554 | { NULL, 0, NULL, 0 } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 555 | }; |
| 556 | int ch; |
| 557 | int opt_ind = 0; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 558 | int flags = BDRV_O_RDWR; |
Max Reitz | 4fbec26 | 2015-02-05 13:58:19 -0500 | [diff] [blame] | 559 | int ret = 0; |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 560 | bool seen_cache = false; |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 561 | bool seen_discard = false; |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 562 | bool seen_aio = false; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 563 | pthread_t client_thread; |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 564 | const char *fmt = NULL; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 565 | Error *local_err = NULL; |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 566 | BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; |
Max Reitz | 4fbec26 | 2015-02-05 13:58:19 -0500 | [diff] [blame] | 567 | QDict *options = NULL; |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 568 | const char *export_name = NULL; /* defaults to "" later for server mode */ |
Eric Blake | b1a75b3 | 2016-10-14 13:33:03 -0500 | [diff] [blame] | 569 | const char *export_description = NULL; |
Vladimir Sementsov-Ogievskiy | e5fb29d | 2022-03-15 00:32:25 +0300 | [diff] [blame] | 570 | BlockDirtyBitmapOrStrList *bitmaps = NULL; |
Eric Blake | dbc7b01 | 2020-10-27 00:05:55 -0500 | [diff] [blame] | 571 | bool alloc_depth = false; |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 572 | const char *tlscredsid = NULL; |
Daniel P. Berrangé | 003b2b2 | 2022-03-04 19:36:02 +0000 | [diff] [blame] | 573 | const char *tlshostname = NULL; |
Daniel P. Berrange | 77c9aae | 2016-02-17 10:10:19 +0000 | [diff] [blame] | 574 | bool imageOpts = false; |
Nir Soffer | 0961525 | 2021-08-13 23:55:19 +0300 | [diff] [blame] | 575 | bool writethrough = false; /* Client will flush as needed. */ |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 576 | bool fork_process = false; |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 577 | bool list = false; |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 578 | int old_stderr = -1; |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 579 | unsigned socket_activation; |
Max Reitz | 637bc5a | 2019-05-08 23:18:16 +0200 | [diff] [blame] | 580 | const char *pid_file_name = NULL; |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 581 | const char *selinux_label = NULL; |
Kevin Wolf | 0091717 | 2020-09-24 17:26:57 +0200 | [diff] [blame] | 582 | BlockExportOptions *export_opts; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 583 | |
Eric Blake | 029a88c | 2020-09-30 07:11:01 -0500 | [diff] [blame] | 584 | #ifdef CONFIG_POSIX |
Stefan Hajnoczi | cbc20bf | 2020-09-29 13:55:15 +0100 | [diff] [blame] | 585 | os_setup_early_signal_handling(); |
| 586 | os_setup_signal_handling(); |
Max Reitz | 041e32b | 2017-06-11 14:37:14 +0200 | [diff] [blame] | 587 | #endif |
| 588 | |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 589 | socket_init(); |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 590 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 591 | module_call_init(MODULE_INIT_TRACE); |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 592 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 593 | |
Daniel P. Berrange | 0ab3b33 | 2016-02-10 18:41:00 +0000 | [diff] [blame] | 594 | module_call_init(MODULE_INIT_QOM); |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 595 | qemu_add_opts(&qemu_trace_opts); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 596 | qemu_init_exec_dir(argv[0]); |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 597 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 598 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 599 | switch (ch) { |
| 600 | case 's': |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 601 | flags |= BDRV_O_SNAPSHOT; |
| 602 | break; |
| 603 | case 'n': |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 604 | optarg = (char *) "none"; |
| 605 | /* fallthrough */ |
| 606 | case QEMU_NBD_OPT_CACHE: |
| 607 | if (seen_cache) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 608 | error_report("-n and --cache can only be specified once"); |
| 609 | exit(EXIT_FAILURE); |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 610 | } |
| 611 | seen_cache = true; |
Kevin Wolf | 6effd5b | 2016-03-14 11:43:28 +0100 | [diff] [blame] | 612 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 613 | error_report("Invalid cache mode `%s'", optarg); |
| 614 | exit(EXIT_FAILURE); |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 615 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 616 | break; |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 617 | case QEMU_NBD_OPT_AIO: |
| 618 | if (seen_aio) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 619 | error_report("--aio can only be specified once"); |
| 620 | exit(EXIT_FAILURE); |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 621 | } |
| 622 | seen_aio = true; |
Aarushi Mehta | 7680274 | 2020-01-20 14:18:56 +0000 | [diff] [blame] | 623 | if (bdrv_parse_aio(optarg, &flags) < 0) { |
| 624 | error_report("Invalid aio mode '%s'", optarg); |
| 625 | exit(EXIT_FAILURE); |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 626 | } |
| 627 | break; |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 628 | case QEMU_NBD_OPT_DISCARD: |
| 629 | if (seen_discard) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 630 | error_report("--discard can only be specified once"); |
| 631 | exit(EXIT_FAILURE); |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 632 | } |
| 633 | seen_discard = true; |
| 634 | if (bdrv_parse_discard_flags(optarg, &flags) == -1) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 635 | error_report("Invalid discard mode `%s'", optarg); |
| 636 | exit(EXIT_FAILURE); |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 637 | } |
| 638 | break; |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 639 | case QEMU_NBD_OPT_DETECT_ZEROES: |
| 640 | detect_zeroes = |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 641 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 642 | optarg, |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 643 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
| 644 | &local_err); |
| 645 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 646 | error_reportf_err(local_err, |
| 647 | "Failed to parse detect_zeroes mode: "); |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 648 | exit(EXIT_FAILURE); |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 649 | } |
| 650 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && |
| 651 | !(flags & BDRV_O_UNMAP)) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 652 | error_report("setting detect-zeroes to unmap is not allowed " |
| 653 | "without setting discard operation to unmap"); |
| 654 | exit(EXIT_FAILURE); |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 655 | } |
| 656 | break; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 657 | case 'b': |
| 658 | bindto = optarg; |
| 659 | break; |
| 660 | case 'p': |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 661 | port = optarg; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 662 | break; |
| 663 | case 'o': |
Eric Blake | 43b5101 | 2019-01-17 13:36:44 -0600 | [diff] [blame] | 664 | if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) { |
| 665 | error_report("Invalid offset '%s'", optarg); |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 666 | exit(EXIT_FAILURE); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 667 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 668 | break; |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 669 | case 'l': |
| 670 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 671 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 672 | optarg, false); |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 673 | if (!sn_opts) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 674 | error_report("Failed in parsing snapshot param `%s'", |
| 675 | optarg); |
| 676 | exit(EXIT_FAILURE); |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 677 | } |
| 678 | } else { |
| 679 | sn_id_or_name = optarg; |
| 680 | } |
| 681 | /* fall through */ |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 682 | case 'r': |
Eric Blake | dbb38ca | 2019-08-23 09:37:22 -0500 | [diff] [blame] | 683 | readonly = true; |
Naphtali Sprei | 07108b2 | 2010-03-14 15:19:57 +0200 | [diff] [blame] | 684 | flags &= ~BDRV_O_RDWR; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 685 | break; |
Eric Blake | dbc7b01 | 2020-10-27 00:05:55 -0500 | [diff] [blame] | 686 | case 'A': |
| 687 | alloc_depth = true; |
| 688 | break; |
Eric Blake | 636192c | 2019-01-11 13:47:20 -0600 | [diff] [blame] | 689 | case 'B': |
Vladimir Sementsov-Ogievskiy | e5fb29d | 2022-03-15 00:32:25 +0300 | [diff] [blame] | 690 | { |
| 691 | BlockDirtyBitmapOrStr *el = g_new(BlockDirtyBitmapOrStr, 1); |
| 692 | *el = (BlockDirtyBitmapOrStr) { |
| 693 | .type = QTYPE_QSTRING, |
| 694 | .u.local = g_strdup(optarg), |
| 695 | }; |
| 696 | QAPI_LIST_PREPEND(bitmaps, el); |
| 697 | } |
Eric Blake | 636192c | 2019-01-11 13:47:20 -0600 | [diff] [blame] | 698 | break; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 699 | case 'k': |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 700 | sockpath = optarg; |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 701 | if (sockpath[0] != '/') { |
Markus Armbruster | 9af9e0f | 2015-12-18 16:35:19 +0100 | [diff] [blame] | 702 | error_report("socket path must be absolute"); |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 703 | exit(EXIT_FAILURE); |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 704 | } |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 705 | break; |
| 706 | case 'd': |
| 707 | disconnect = true; |
| 708 | break; |
| 709 | case 'c': |
| 710 | device = optarg; |
| 711 | break; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 712 | case 'e': |
Eric Blake | 43b5101 | 2019-01-17 13:36:44 -0600 | [diff] [blame] | 713 | if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 || |
Eric Blake | 3dcf56e | 2021-02-09 09:27:59 -0600 | [diff] [blame] | 714 | shared < 0) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 715 | error_report("Invalid shared device number '%s'", optarg); |
| 716 | exit(EXIT_FAILURE); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 717 | } |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 718 | break; |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 719 | case 'f': |
| 720 | fmt = optarg; |
| 721 | break; |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 722 | case 't': |
| 723 | persistent = 1; |
| 724 | break; |
Daniel P. Berrange | 3d4b2f9 | 2016-02-10 18:41:08 +0000 | [diff] [blame] | 725 | case 'x': |
| 726 | export_name = optarg; |
Eric Blake | 93676c8 | 2019-11-13 20:46:34 -0600 | [diff] [blame] | 727 | if (strlen(export_name) > NBD_MAX_STRING_SIZE) { |
| 728 | error_report("export name '%s' too long", export_name); |
| 729 | exit(EXIT_FAILURE); |
| 730 | } |
Daniel P. Berrange | 3d4b2f9 | 2016-02-10 18:41:08 +0000 | [diff] [blame] | 731 | break; |
Eric Blake | b1a75b3 | 2016-10-14 13:33:03 -0500 | [diff] [blame] | 732 | case 'D': |
| 733 | export_description = optarg; |
Eric Blake | 93676c8 | 2019-11-13 20:46:34 -0600 | [diff] [blame] | 734 | if (strlen(export_description) > NBD_MAX_STRING_SIZE) { |
| 735 | error_report("export description '%s' too long", |
| 736 | export_description); |
| 737 | exit(EXIT_FAILURE); |
| 738 | } |
Eric Blake | b1a75b3 | 2016-10-14 13:33:03 -0500 | [diff] [blame] | 739 | break; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 740 | case 'v': |
| 741 | verbose = 1; |
| 742 | break; |
| 743 | case 'V': |
| 744 | version(argv[0]); |
| 745 | exit(0); |
| 746 | break; |
| 747 | case 'h': |
| 748 | usage(argv[0]); |
| 749 | exit(0); |
| 750 | break; |
| 751 | case '?': |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 752 | error_report("Try `%s --help' for more information.", argv[0]); |
| 753 | exit(EXIT_FAILURE); |
Kevin Wolf | fa40e43 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 754 | case QEMU_NBD_OPT_OBJECT: |
| 755 | user_creatable_process_cmdline(optarg); |
| 756 | break; |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 757 | case QEMU_NBD_OPT_TLSCREDS: |
| 758 | tlscredsid = optarg; |
| 759 | break; |
Daniel P. Berrangé | 003b2b2 | 2022-03-04 19:36:02 +0000 | [diff] [blame] | 760 | case QEMU_NBD_OPT_TLSHOSTNAME: |
| 761 | tlshostname = optarg; |
| 762 | break; |
Daniel P. Berrange | 77c9aae | 2016-02-17 10:10:19 +0000 | [diff] [blame] | 763 | case QEMU_NBD_OPT_IMAGE_OPTS: |
| 764 | imageOpts = true; |
| 765 | break; |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 766 | case 'T': |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 767 | trace_opt_parse(optarg); |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 768 | break; |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 769 | case QEMU_NBD_OPT_TLSAUTHZ: |
| 770 | tlsauthz = optarg; |
| 771 | break; |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 772 | case QEMU_NBD_OPT_FORK: |
| 773 | fork_process = true; |
| 774 | break; |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 775 | case 'L': |
| 776 | list = true; |
| 777 | break; |
Max Reitz | 637bc5a | 2019-05-08 23:18:16 +0200 | [diff] [blame] | 778 | case QEMU_NBD_OPT_PID_FILE: |
| 779 | pid_file_name = optarg; |
| 780 | break; |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 781 | case QEMU_NBD_OPT_SELINUX_LABEL: |
| 782 | selinux_label = optarg; |
| 783 | break; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 787 | if (list) { |
| 788 | if (argc != optind) { |
| 789 | error_report("List mode is incompatible with a file name"); |
| 790 | exit(EXIT_FAILURE); |
| 791 | } |
Eric Blake | 0bc1699 | 2020-01-23 10:46:50 -0600 | [diff] [blame] | 792 | if (export_name || export_description || dev_offset || |
Eric Blake | cbad81c | 2020-10-27 00:05:49 -0500 | [diff] [blame] | 793 | device || disconnect || fmt || sn_id_or_name || bitmaps || |
Eric Blake | dbc7b01 | 2020-10-27 00:05:55 -0500 | [diff] [blame] | 794 | alloc_depth || seen_aio || seen_discard || seen_cache) { |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 795 | error_report("List mode is incompatible with per-device settings"); |
| 796 | exit(EXIT_FAILURE); |
| 797 | } |
| 798 | if (fork_process) { |
| 799 | error_report("List mode is incompatible with forking"); |
| 800 | exit(EXIT_FAILURE); |
| 801 | } |
| 802 | } else if ((argc - optind) != 1) { |
Markus Armbruster | 433672b | 2015-12-18 16:35:24 +0100 | [diff] [blame] | 803 | error_report("Invalid number of arguments"); |
| 804 | error_printf("Try `%s --help' for more information.\n", argv[0]); |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 805 | exit(EXIT_FAILURE); |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 806 | } else if (!export_name) { |
| 807 | export_name = ""; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 810 | if (!trace_init_backends()) { |
| 811 | exit(1); |
| 812 | } |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 813 | trace_init_file(); |
Richard Henderson | c5955f4 | 2022-04-17 11:29:44 -0700 | [diff] [blame] | 814 | qemu_set_log(LOG_TRACE, &error_fatal); |
Denis V. Lunev | 39ca463 | 2016-06-17 17:44:12 +0300 | [diff] [blame] | 815 | |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 816 | socket_activation = check_socket_activation(); |
| 817 | if (socket_activation == 0) { |
Daniel P. Berrangé | e8ae8b1 | 2022-03-04 19:36:03 +0000 | [diff] [blame] | 818 | if (!sockpath) { |
| 819 | setup_address_and_port(&bindto, &port); |
| 820 | } |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 821 | } else { |
| 822 | /* Using socket activation - check user didn't use -p etc. */ |
| 823 | const char *err_msg = socket_activation_validate_opts(device, sockpath, |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 824 | bindto, port, |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 825 | selinux_label, |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 826 | list); |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 827 | if (err_msg != NULL) { |
| 828 | error_report("%s", err_msg); |
| 829 | exit(EXIT_FAILURE); |
| 830 | } |
Paolo Bonzini | 53fabd4 | 2017-03-16 16:29:45 +0100 | [diff] [blame] | 831 | |
| 832 | /* qemu-nbd can only listen on a single socket. */ |
| 833 | if (socket_activation > 1) { |
| 834 | error_report("qemu-nbd does not support socket activation with %s > 1", |
| 835 | "LISTEN_FDS"); |
| 836 | exit(EXIT_FAILURE); |
| 837 | } |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 840 | if (tlscredsid) { |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 841 | if (device) { |
| 842 | error_report("TLS is not supported with a host device"); |
| 843 | exit(EXIT_FAILURE); |
| 844 | } |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 845 | if (tlsauthz && list) { |
| 846 | error_report("TLS authorization is incompatible with export list"); |
| 847 | exit(EXIT_FAILURE); |
| 848 | } |
Daniel P. Berrangé | 003b2b2 | 2022-03-04 19:36:02 +0000 | [diff] [blame] | 849 | if (tlshostname && !list) { |
| 850 | error_report("TLS hostname is only supported with export list"); |
| 851 | exit(EXIT_FAILURE); |
| 852 | } |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 853 | tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err); |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 854 | if (local_err) { |
Markus Armbruster | 5217f18 | 2020-05-05 12:19:03 +0200 | [diff] [blame] | 855 | error_reportf_err(local_err, "Failed to get TLS creds: "); |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 856 | exit(EXIT_FAILURE); |
| 857 | } |
Daniel P. Berrange | b25e12d | 2019-02-27 16:20:33 +0000 | [diff] [blame] | 858 | } else { |
| 859 | if (tlsauthz) { |
| 860 | error_report("--tls-authz is not permitted without --tls-creds"); |
| 861 | exit(EXIT_FAILURE); |
| 862 | } |
Daniel P. Berrangé | 003b2b2 | 2022-03-04 19:36:02 +0000 | [diff] [blame] | 863 | if (tlshostname) { |
| 864 | error_report("--tls-hostname is not permitted without --tls-creds"); |
| 865 | exit(EXIT_FAILURE); |
| 866 | } |
Daniel P. Berrange | 145614a | 2016-02-10 18:41:13 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 869 | if (selinux_label) { |
| 870 | #ifdef CONFIG_SELINUX |
| 871 | if (sockpath == NULL && device == NULL) { |
| 872 | error_report("--selinux-label is not permitted without --socket"); |
| 873 | exit(EXIT_FAILURE); |
| 874 | } |
| 875 | #else |
| 876 | error_report("SELinux support not enabled in this binary"); |
| 877 | exit(EXIT_FAILURE); |
| 878 | #endif |
| 879 | } |
| 880 | |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 881 | if (list) { |
| 882 | saddr = nbd_build_socket_address(sockpath, bindto, port); |
Daniel P. Berrangé | 003b2b2 | 2022-03-04 19:36:02 +0000 | [diff] [blame] | 883 | return qemu_nbd_client_list(saddr, tlscreds, |
| 884 | tlshostname ? tlshostname : bindto); |
Eric Blake | 68b96f1 | 2019-01-17 13:36:56 -0600 | [diff] [blame] | 885 | } |
| 886 | |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 887 | #if !HAVE_NBD_DEVICE |
| 888 | if (disconnect || device) { |
| 889 | error_report("Kernel /dev/nbdN support not available"); |
| 890 | exit(EXIT_FAILURE); |
| 891 | } |
| 892 | #else /* HAVE_NBD_DEVICE */ |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 893 | if (disconnect) { |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 894 | int nbdfd = open(argv[optind], O_RDWR); |
| 895 | if (nbdfd < 0) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 896 | error_report("Cannot open %s: %s", argv[optind], |
| 897 | strerror(errno)); |
| 898 | exit(EXIT_FAILURE); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 899 | } |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 900 | nbd_disconnect(nbdfd); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 901 | |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 902 | close(nbdfd); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 903 | |
| 904 | printf("%s disconnected\n", argv[optind]); |
| 905 | |
Peter Lieven | 713cc67 | 2014-08-13 19:20:19 +0200 | [diff] [blame] | 906 | return 0; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 907 | } |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 908 | #endif |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 909 | |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 910 | if ((device && !verbose) || fork_process) { |
Daniel P. Berrangé | eb70598 | 2020-08-25 11:38:50 +0100 | [diff] [blame] | 911 | #ifndef WIN32 |
Marc-André Lureau | a724197 | 2022-03-29 15:21:00 +0400 | [diff] [blame] | 912 | g_autoptr(GError) err = NULL; |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 913 | int stderr_fd[2]; |
| 914 | pid_t pid; |
| 915 | int ret; |
| 916 | |
Marc-André Lureau | a724197 | 2022-03-29 15:21:00 +0400 | [diff] [blame] | 917 | if (!g_unix_open_pipe(stderr_fd, FD_CLOEXEC, &err)) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 918 | error_report("Error setting up communication pipe: %s", |
Marc-André Lureau | a724197 | 2022-03-29 15:21:00 +0400 | [diff] [blame] | 919 | err->message); |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 920 | exit(EXIT_FAILURE); |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* Now daemonize, but keep a communication channel open to |
| 924 | * print errors and exit with the proper status code. |
| 925 | */ |
| 926 | pid = fork(); |
Max Reitz | 70d4739 | 2015-02-25 13:08:22 -0500 | [diff] [blame] | 927 | if (pid < 0) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 928 | error_report("Failed to fork: %s", strerror(errno)); |
| 929 | exit(EXIT_FAILURE); |
Max Reitz | 70d4739 | 2015-02-25 13:08:22 -0500 | [diff] [blame] | 930 | } else if (pid == 0) { |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 931 | close(stderr_fd[0]); |
Max Reitz | e6df58a | 2019-05-08 23:18:18 +0200 | [diff] [blame] | 932 | |
Raphael Pour | 0eaf453 | 2020-05-15 08:36:07 +0200 | [diff] [blame] | 933 | /* Remember parent's stderr if we will be restoring it. */ |
| 934 | if (fork_process) { |
| 935 | old_stderr = dup(STDERR_FILENO); |
| 936 | } |
| 937 | |
Michael Tokarev | 9faf31b | 2012-01-16 18:37:44 +0400 | [diff] [blame] | 938 | ret = qemu_daemon(1, 0); |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 939 | |
| 940 | /* Temporarily redirect stderr to the parent's pipe... */ |
| 941 | dup2(stderr_fd[1], STDERR_FILENO); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 942 | if (ret < 0) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 943 | error_report("Failed to daemonize: %s", strerror(errno)); |
| 944 | exit(EXIT_FAILURE); |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /* ... close the descriptor we inherited and go on. */ |
| 948 | close(stderr_fd[1]); |
| 949 | } else { |
| 950 | bool errors = false; |
| 951 | char *buf; |
| 952 | |
| 953 | /* In the parent. Print error messages from the child until |
| 954 | * it closes the pipe. |
| 955 | */ |
| 956 | close(stderr_fd[1]); |
| 957 | buf = g_malloc(1024); |
| 958 | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { |
| 959 | errors = true; |
| 960 | ret = qemu_write_full(STDERR_FILENO, buf, ret); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 961 | if (ret < 0) { |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 962 | exit(EXIT_FAILURE); |
| 963 | } |
| 964 | } |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 965 | if (ret < 0) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 966 | error_report("Cannot read from daemon: %s", |
| 967 | strerror(errno)); |
| 968 | exit(EXIT_FAILURE); |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* Usually the daemon should not print any message. |
| 972 | * Exit with zero status in that case. |
| 973 | */ |
| 974 | exit(errors); |
| 975 | } |
Daniel P. Berrangé | eb70598 | 2020-08-25 11:38:50 +0100 | [diff] [blame] | 976 | #else /* WIN32 */ |
| 977 | error_report("Unable to fork into background on Windows hosts"); |
| 978 | exit(EXIT_FAILURE); |
| 979 | #endif /* WIN32 */ |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 982 | if (device != NULL && sockpath == NULL) { |
| 983 | sockpath = g_malloc(128); |
| 984 | snprintf(sockpath, 128, SOCKET_PATH, basename(device)); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 987 | server = qio_net_listener_new(); |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 988 | if (socket_activation == 0) { |
Eric Blake | 582d421 | 2021-02-09 09:27:58 -0600 | [diff] [blame] | 989 | int backlog; |
| 990 | |
Eric Blake | 3dcf56e | 2021-02-09 09:27:59 -0600 | [diff] [blame] | 991 | if (persistent || shared == 0) { |
Eric Blake | 582d421 | 2021-02-09 09:27:58 -0600 | [diff] [blame] | 992 | backlog = SOMAXCONN; |
| 993 | } else { |
| 994 | backlog = MIN(shared, SOMAXCONN); |
| 995 | } |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 996 | #ifdef CONFIG_SELINUX |
| 997 | if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) { |
| 998 | error_report("Cannot set SELinux socket create context to %s: %s", |
| 999 | selinux_label, strerror(errno)); |
| 1000 | exit(EXIT_FAILURE); |
| 1001 | } |
| 1002 | #endif |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 1003 | saddr = nbd_build_socket_address(sockpath, bindto, port); |
Eric Blake | 582d421 | 2021-02-09 09:27:58 -0600 | [diff] [blame] | 1004 | if (qio_net_listener_open_sync(server, saddr, backlog, |
| 1005 | &local_err) < 0) { |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 1006 | object_unref(OBJECT(server)); |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 1007 | error_report_err(local_err); |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 1008 | exit(EXIT_FAILURE); |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 1009 | } |
Richard W.M. Jones | 3d212b4 | 2021-11-15 14:29:43 -0600 | [diff] [blame] | 1010 | #ifdef CONFIG_SELINUX |
| 1011 | if (selinux_label && setsockcreatecon_raw(NULL) == -1) { |
| 1012 | error_report("Cannot clear SELinux socket create context: %s", |
| 1013 | strerror(errno)); |
| 1014 | exit(EXIT_FAILURE); |
| 1015 | } |
| 1016 | #endif |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 1017 | } else { |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 1018 | size_t i; |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 1019 | /* See comment in check_socket_activation above. */ |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 1020 | for (i = 0; i < socket_activation; i++) { |
| 1021 | QIOChannelSocket *sioc; |
| 1022 | sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i, |
| 1023 | &local_err); |
| 1024 | if (sioc == NULL) { |
| 1025 | object_unref(OBJECT(server)); |
Markus Armbruster | 5217f18 | 2020-05-05 12:19:03 +0200 | [diff] [blame] | 1026 | error_reportf_err(local_err, |
| 1027 | "Failed to use socket activation: "); |
Daniel P. Berrange | e4849c1 | 2017-12-18 10:16:43 +0000 | [diff] [blame] | 1028 | exit(EXIT_FAILURE); |
| 1029 | } |
| 1030 | qio_net_listener_add(server, sioc); |
| 1031 | object_unref(OBJECT(sioc)); |
Richard W.M. Jones | a721f53 | 2017-02-04 10:03:17 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
Daniel P. Berrange | 48bec07 | 2015-09-16 14:52:23 +0100 | [diff] [blame] | 1034 | |
Markus Armbruster | f9734d5 | 2021-07-20 14:53:53 +0200 | [diff] [blame] | 1035 | qemu_init_main_loop(&error_fatal); |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 1036 | bdrv_init(); |
Kevin Wolf | b3b5299 | 2018-05-16 13:46:37 +0200 | [diff] [blame] | 1037 | atexit(qemu_nbd_shutdown); |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 1038 | |
Daniel P. Berrange | 77c9aae | 2016-02-17 10:10:19 +0000 | [diff] [blame] | 1039 | srcpath = argv[optind]; |
| 1040 | if (imageOpts) { |
| 1041 | QemuOpts *opts; |
| 1042 | if (fmt) { |
| 1043 | error_report("--image-opts and -f are mutually exclusive"); |
| 1044 | exit(EXIT_FAILURE); |
| 1045 | } |
| 1046 | opts = qemu_opts_parse_noisily(&file_opts, srcpath, true); |
| 1047 | if (!opts) { |
| 1048 | qemu_opts_reset(&file_opts); |
| 1049 | exit(EXIT_FAILURE); |
| 1050 | } |
| 1051 | options = qemu_opts_to_qdict(opts, NULL); |
| 1052 | qemu_opts_reset(&file_opts); |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 1053 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | 77c9aae | 2016-02-17 10:10:19 +0000 | [diff] [blame] | 1054 | } else { |
| 1055 | if (fmt) { |
| 1056 | options = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 1057 | qdict_put_str(options, "driver", fmt); |
Daniel P. Berrange | 77c9aae | 2016-02-17 10:10:19 +0000 | [diff] [blame] | 1058 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 1059 | blk = blk_new_open(srcpath, NULL, options, flags, &local_err); |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Max Reitz | 4fbec26 | 2015-02-05 13:58:19 -0500 | [diff] [blame] | 1062 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 1063 | error_reportf_err(local_err, "Failed to blk_new_open '%s': ", |
| 1064 | argv[optind]); |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 1065 | exit(EXIT_FAILURE); |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 1066 | } |
Max Reitz | 4fbec26 | 2015-02-05 13:58:19 -0500 | [diff] [blame] | 1067 | bs = blk_bs(blk); |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 1068 | |
Kevin Wolf | b57e4de | 2020-09-24 17:26:52 +0200 | [diff] [blame] | 1069 | if (dev_offset) { |
| 1070 | QDict *raw_opts = qdict_new(); |
| 1071 | qdict_put_str(raw_opts, "driver", "raw"); |
| 1072 | qdict_put_str(raw_opts, "file", bs->node_name); |
| 1073 | qdict_put_int(raw_opts, "offset", dev_offset); |
| 1074 | bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal); |
| 1075 | blk_remove_bs(blk); |
| 1076 | blk_insert_bs(blk, bs, &error_fatal); |
| 1077 | bdrv_unref(bs); |
| 1078 | } |
| 1079 | |
Kevin Wolf | 6effd5b | 2016-03-14 11:43:28 +0100 | [diff] [blame] | 1080 | blk_set_enable_write_cache(blk, !writethrough); |
| 1081 | |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 1082 | if (sn_opts) { |
| 1083 | ret = bdrv_snapshot_load_tmp(bs, |
| 1084 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 1085 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 1086 | &local_err); |
| 1087 | } else if (sn_id_or_name) { |
| 1088 | ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name, |
| 1089 | &local_err); |
| 1090 | } |
| 1091 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 1092 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 1093 | exit(EXIT_FAILURE); |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Peter Lieven | b3838a4 | 2014-08-13 19:20:18 +0200 | [diff] [blame] | 1096 | bs->detect_zeroes = detect_zeroes; |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 1097 | |
Eric Blake | a5fced4 | 2022-05-11 19:49:23 -0500 | [diff] [blame] | 1098 | nbd_server_is_qemu_nbd(shared); |
Kevin Wolf | 0091717 | 2020-09-24 17:26:57 +0200 | [diff] [blame] | 1099 | |
| 1100 | export_opts = g_new(BlockExportOptions, 1); |
| 1101 | *export_opts = (BlockExportOptions) { |
| 1102 | .type = BLOCK_EXPORT_TYPE_NBD, |
Kevin Wolf | d53be9c | 2020-09-24 17:27:04 +0200 | [diff] [blame] | 1103 | .id = g_strdup("qemu-nbd-export"), |
Kevin Wolf | b6076af | 2020-09-24 17:27:01 +0200 | [diff] [blame] | 1104 | .node_name = g_strdup(bdrv_get_node_name(bs)), |
Kevin Wolf | 0091717 | 2020-09-24 17:26:57 +0200 | [diff] [blame] | 1105 | .has_writethrough = true, |
| 1106 | .writethrough = writethrough, |
Kevin Wolf | 30dbc81 | 2020-09-24 17:27:11 +0200 | [diff] [blame] | 1107 | .has_writable = true, |
| 1108 | .writable = !readonly, |
Kevin Wolf | 0091717 | 2020-09-24 17:26:57 +0200 | [diff] [blame] | 1109 | .u.nbd = { |
Eric Blake | cbad81c | 2020-10-27 00:05:49 -0500 | [diff] [blame] | 1110 | .has_name = true, |
| 1111 | .name = g_strdup(export_name), |
| 1112 | .has_description = !!export_description, |
| 1113 | .description = g_strdup(export_description), |
| 1114 | .has_bitmaps = !!bitmaps, |
| 1115 | .bitmaps = bitmaps, |
Eric Blake | dbc7b01 | 2020-10-27 00:05:55 -0500 | [diff] [blame] | 1116 | .has_allocation_depth = alloc_depth, |
| 1117 | .allocation_depth = alloc_depth, |
Kevin Wolf | 0091717 | 2020-09-24 17:26:57 +0200 | [diff] [blame] | 1118 | }, |
| 1119 | }; |
| 1120 | blk_exp_add(export_opts, &error_fatal); |
| 1121 | qapi_free_BlockExportOptions(export_opts); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 1122 | |
Paolo Bonzini | f1ef555 | 2011-11-04 15:51:23 +0100 | [diff] [blame] | 1123 | if (device) { |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 1124 | #if HAVE_NBD_DEVICE |
Paolo Bonzini | f1ef555 | 2011-11-04 15:51:23 +0100 | [diff] [blame] | 1125 | int ret; |
| 1126 | |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 1127 | ret = pthread_create(&client_thread, NULL, nbd_client_thread, device); |
Paolo Bonzini | f1ef555 | 2011-11-04 15:51:23 +0100 | [diff] [blame] | 1128 | if (ret != 0) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 1129 | error_report("Failed to create client thread: %s", strerror(ret)); |
| 1130 | exit(EXIT_FAILURE); |
Paolo Bonzini | f1ef555 | 2011-11-04 15:51:23 +0100 | [diff] [blame] | 1131 | } |
Eric Blake | 3c1fa35 | 2018-12-15 07:53:08 -0600 | [diff] [blame] | 1132 | #endif |
Paolo Bonzini | f1ef555 | 2011-11-04 15:51:23 +0100 | [diff] [blame] | 1133 | } else { |
| 1134 | /* Shut up GCC warnings. */ |
| 1135 | memset(&client_thread, 0, sizeof(client_thread)); |
| 1136 | } |
| 1137 | |
Daniel P. Berrange | d0d6ff5 | 2016-02-10 18:41:02 +0000 | [diff] [blame] | 1138 | nbd_update_server_watch(); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 1139 | |
Max Reitz | 637bc5a | 2019-05-08 23:18:16 +0200 | [diff] [blame] | 1140 | if (pid_file_name) { |
| 1141 | qemu_write_pidfile(pid_file_name, &error_fatal); |
| 1142 | } |
| 1143 | |
Michael Tokarev | 9faf31b | 2012-01-16 18:37:44 +0400 | [diff] [blame] | 1144 | /* now when the initialization is (almost) complete, chdir("/") |
| 1145 | * to free any busy filesystems */ |
| 1146 | if (chdir("/") < 0) { |
Markus Armbruster | 85b01e0 | 2015-12-18 16:35:04 +0100 | [diff] [blame] | 1147 | error_report("Could not chdir to root directory: %s", |
| 1148 | strerror(errno)); |
| 1149 | exit(EXIT_FAILURE); |
Michael Tokarev | 9faf31b | 2012-01-16 18:37:44 +0400 | [diff] [blame] | 1150 | } |
| 1151 | |
Max Reitz | ffb31e1 | 2016-09-28 22:46:42 +0200 | [diff] [blame] | 1152 | if (fork_process) { |
| 1153 | dup2(old_stderr, STDERR_FILENO); |
| 1154 | close(old_stderr); |
| 1155 | } |
| 1156 | |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 1157 | state = RUNNING; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 1158 | do { |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 1159 | main_loop_wait(false); |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 1160 | if (state == TERMINATE) { |
Kevin Wolf | bc4ee65 | 2020-09-24 17:27:03 +0200 | [diff] [blame] | 1161 | blk_exp_close_all(); |
Kevin Wolf | d794f7f | 2020-09-24 17:26:56 +0200 | [diff] [blame] | 1162 | state = TERMINATED; |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 1163 | } |
| 1164 | } while (state != TERMINATED); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 1165 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1166 | blk_unref(blk); |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 1167 | if (sockpath) { |
| 1168 | unlink(sockpath); |
| 1169 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 1170 | |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 1171 | qemu_opts_del(sn_opts); |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 1172 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 1173 | if (device) { |
| 1174 | void *ret; |
| 1175 | pthread_join(client_thread, &ret); |
| 1176 | exit(ret != NULL); |
| 1177 | } else { |
| 1178 | exit(EXIT_SUCCESS); |
| 1179 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 1180 | } |