blob: 6554f0ab65ee9d4030a27f838dc8d4b1b1821d12 [file] [log] [blame]
thscd831bd2008-07-03 10:23:51 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * 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 Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard7a5ca862008-05-27 21:13:40 +000017 */
18
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Stefan Weil5a61cb62011-09-08 17:55:32 +020021#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020022#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020023#include "sysemu/block-backend.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020024#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010025#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010026#include "qemu/main-loop.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010027#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000028#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010029#include "qemu/bswap.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080030#include "block/snapshot.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020031#include "qapi/util.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010032#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000033#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000034#include "io/channel-socket.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010035#include "crypto/init.h"
bellard7a5ca862008-05-27 21:13:40 +000036
bellard7a5ca862008-05-27 21:13:40 +000037#include <getopt.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000038#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010039#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000040
Peter Lieven713cc672014-08-13 19:20:19 +020041#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000042#define QEMU_NBD_OPT_CACHE 256
43#define QEMU_NBD_OPT_AIO 257
44#define QEMU_NBD_OPT_DISCARD 258
45#define QEMU_NBD_OPT_DETECT_ZEROES 259
46#define QEMU_NBD_OPT_OBJECT 260
47#define QEMU_NBD_OPT_TLSCREDS 261
48#define QEMU_NBD_OPT_IMAGE_OPTS 262
bellard7a5ca862008-05-27 21:13:40 +000049
Eric Blakebd31c212016-05-06 10:26:42 -060050#define MBR_SIZE 512
51
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020052static NBDExport *exp;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +000053static bool newproto;
blueswir1b1d8e522008-10-26 13:43:07 +000054static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010055static char *srcpath;
Daniel P. Berrange48bec072015-09-16 14:52:23 +010056static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020057static int persistent = 0;
58static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020059static int shared = 1;
60static int nb_fds;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000061static QIOChannelSocket *server_ioc;
62static int server_watch = -1;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000063static QCryptoTLSCreds *tlscreds;
bellard7a5ca862008-05-27 21:13:40 +000064
65static void usage(const char *name)
66{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020067 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000068"Usage: %s [OPTIONS] FILE\n"
69"QEMU Disk Network Block Device Server\n"
70"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020071" -h, --help display this help and exit\n"
72" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000073"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020074"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020075" -p, --port=PORT port to listen on (default `%d')\n"
76" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
77" -k, --socket=PATH path to the unix socket\n"
78" (default '"SOCKET_PATH"')\n"
79" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
80" -t, --persistent don't exit on the last connection\n"
81" -v, --verbose display extra debugging information\n"
Eric Blake332a2542016-04-05 20:02:08 -060082" -x, --export-name=NAME expose export by name\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020083"\n"
84"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020085" -o, --offset=OFFSET offset into the image\n"
86" -P, --partition=NUM only expose partition NUM\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020087"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000088"General purpose options:\n"
89" --object type,id=ID,... define an object such as 'secret' for providing\n"
90" passwords and/or encryption keys\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020091#ifdef __linux__
92"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020093" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
94" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020095"\n"
96#endif
97"\n"
98"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020099" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
100" -r, --read-only export read-only\n"
101" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
102" file with backing_file=FILE, redirect the write to\n"
103" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800104" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200105" load an internal snapshot inside FILE and export it\n"
106" as an read-only device, SNAPSHOT_PARAM format is\n"
107" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
108" '[ID_OR_NAME]'\n"
109" -n, --nocache disable host cache\n"
110" --cache=MODE set cache mode (none, writeback, ...)\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200111" --aio=MODE set AIO mode (native or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200112" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300113" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000114" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200115"\n"
116"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +0200117 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000118}
119
120static void version(const char *name)
121{
122 printf(
ths315bc7a2008-07-18 18:06:23 +0000123"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +0000124"Written by Anthony Liguori.\n"
125"\n"
126"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
127"This is free software; see the source for copying conditions. There is NO\n"
128"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000129 , name);
bellard7a5ca862008-05-27 21:13:40 +0000130}
131
132struct partition_record
133{
134 uint8_t bootable;
135 uint8_t start_head;
136 uint32_t start_cylinder;
137 uint8_t start_sector;
138 uint8_t system;
139 uint8_t end_head;
140 uint8_t end_cylinder;
141 uint8_t end_sector;
142 uint32_t start_sector_abs;
143 uint32_t nb_sectors_abs;
144};
145
146static void read_partition(uint8_t *p, struct partition_record *r)
147{
148 r->bootable = p[0];
149 r->start_head = p[1];
150 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
151 r->start_sector = p[2] & 0x3f;
152 r->system = p[4];
153 r->end_head = p[5];
154 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
155 r->end_sector = p[6] & 0x3f;
Max Reitzac973932015-02-25 13:08:23 -0500156
157 r->start_sector_abs = le32_to_cpup((uint32_t *)(p + 8));
158 r->nb_sectors_abs = le32_to_cpup((uint32_t *)(p + 12));
bellard7a5ca862008-05-27 21:13:40 +0000159}
160
Max Reitz4c58e802014-11-18 12:21:19 +0100161static int find_partition(BlockBackend *blk, int partition,
bellard7a5ca862008-05-27 21:13:40 +0000162 off_t *offset, off_t *size)
163{
164 struct partition_record mbr[4];
Eric Blakebd31c212016-05-06 10:26:42 -0600165 uint8_t data[MBR_SIZE];
bellard7a5ca862008-05-27 21:13:40 +0000166 int i;
167 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900168 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000169
Eric Blakebd31c212016-05-06 10:26:42 -0600170 ret = blk_pread(blk, 0, data, sizeof(data));
171 if (ret < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100172 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100173 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900174 }
bellard7a5ca862008-05-27 21:13:40 +0000175
176 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100177 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000178 }
179
180 for (i = 0; i < 4; i++) {
181 read_partition(&data[446 + 16 * i], &mbr[i]);
182
Max Reitz453b07b2015-02-25 13:08:15 -0500183 if (!mbr[i].system || !mbr[i].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000184 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500185 }
bellard7a5ca862008-05-27 21:13:40 +0000186
187 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
188 struct partition_record ext[4];
Eric Blakebd31c212016-05-06 10:26:42 -0600189 uint8_t data1[MBR_SIZE];
bellard7a5ca862008-05-27 21:13:40 +0000190 int j;
191
Eric Blakebd31c212016-05-06 10:26:42 -0600192 ret = blk_pread(blk, mbr[i].start_sector_abs * MBR_SIZE,
193 data1, sizeof(data1));
194 if (ret < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100195 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100196 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900197 }
bellard7a5ca862008-05-27 21:13:40 +0000198
199 for (j = 0; j < 4; j++) {
200 read_partition(&data1[446 + 16 * j], &ext[j]);
Max Reitz453b07b2015-02-25 13:08:15 -0500201 if (!ext[j].system || !ext[j].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000202 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500203 }
bellard7a5ca862008-05-27 21:13:40 +0000204
205 if ((ext_partnum + j + 1) == partition) {
206 *offset = (uint64_t)ext[j].start_sector_abs << 9;
207 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
208 return 0;
209 }
210 }
211 ext_partnum += 4;
212 } else if ((i + 1) == partition) {
213 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
214 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
215 return 0;
216 }
217 }
218
Paolo Bonzini185b4332012-03-05 08:56:10 +0100219 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000220}
221
Paolo Bonzinibb345112011-11-04 15:51:19 +0100222static void termsig_handler(int signum)
223{
Pavel Butsykin23994a52016-04-14 13:20:15 +0300224 atomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200225 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100226}
227
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100228
Paolo Bonzinia517e882011-11-04 15:51:21 +0100229static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000230{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100231 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100232 int nbd;
thscd831bd2008-07-03 10:23:51 +0000233
Paolo Bonzinia517e882011-11-04 15:51:21 +0100234 /* linux just needs an open() to trigger
235 * the partition table update
236 * but remember to load the module with max_part != 0 :
237 * modprobe nbd max_part=63
238 */
239 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100240 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100241 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000242 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100243 return NULL;
244}
245
246static void *nbd_client_thread(void *arg)
247{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100248 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100249 off_t size;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100250 uint32_t nbdflags;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000251 QIOChannelSocket *sioc;
252 int fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100253 int ret;
254 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500255 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100256
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000257 sioc = qio_channel_socket_new();
258 if (qio_channel_socket_connect_sync(sioc,
259 saddr,
260 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100261 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000262 goto out;
263 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100264
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +0000265 ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL, &nbdflags,
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000266 NULL, NULL, NULL,
Max Reitz3f472652015-02-25 13:08:25 -0500267 &size, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100268 if (ret < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500269 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100270 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500271 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100272 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100273 }
274
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100275 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100276 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100277 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100278 error_report("Failed to open %s: %m", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100279 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100280 }
281
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +0000282 ret = nbd_init(fd, sioc, nbdflags, size);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100283 if (ret < 0) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100284 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100285 }
286
287 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100288 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100289
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100290 if (verbose) {
291 fprintf(stderr, "NBD device %s is now connected to %s\n",
292 device, srcpath);
293 } else {
294 /* Close stderr so that the qemu-nbd process exits. */
295 dup2(STDOUT_FILENO, STDERR_FILENO);
296 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100297
298 ret = nbd_client(fd);
299 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100300 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100301 }
302 close(fd);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000303 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100304 kill(getpid(), SIGTERM);
305 return (void *) EXIT_SUCCESS;
306
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100307out_fd:
308 close(fd);
309out_socket:
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000310 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311out:
312 kill(getpid(), SIGTERM);
313 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000314}
315
Fam Zhenge4afbf42015-05-19 10:50:59 +0000316static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200317{
318 return nb_fds < shared;
319}
320
Paolo Bonzini7860a382012-09-18 13:31:56 +0200321static void nbd_export_closed(NBDExport *exp)
322{
323 assert(state == TERMINATING);
324 state = TERMINATED;
325}
326
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000327static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000328
Paolo Bonzini1743b512011-09-19 14:33:23 +0200329static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200330{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200331 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200332 if (nb_fds == 0 && !persistent && state == RUNNING) {
333 state = TERMINATE;
334 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000335 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200336 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200337}
338
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000339static gboolean nbd_accept(QIOChannel *ioc, GIOCondition cond, gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200340{
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000341 QIOChannelSocket *cioc;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200342
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000343 cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
344 NULL);
345 if (!cioc) {
346 return TRUE;
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100347 }
348
Paolo Bonzini7860a382012-09-18 13:31:56 +0200349 if (state >= TERMINATE) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000350 object_unref(OBJECT(cioc));
351 return TRUE;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200352 }
353
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800354 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000355 nbd_update_server_watch();
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000356 nbd_client_new(newproto ? NULL : exp, cioc,
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000357 tlscreds, NULL, nbd_client_closed);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000358 object_unref(OBJECT(cioc));
359
360 return TRUE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200361}
362
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000363static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000364{
365 if (nbd_can_accept()) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000366 if (server_watch == -1) {
367 server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc),
368 G_IO_IN,
369 nbd_accept,
370 NULL, NULL);
371 }
Fam Zhenge4afbf42015-05-19 10:50:59 +0000372 } else {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000373 if (server_watch != -1) {
374 g_source_remove(server_watch);
375 server_watch = -1;
376 }
Fam Zhenge4afbf42015-05-19 10:50:59 +0000377 }
378}
379
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100380
381static SocketAddress *nbd_build_socket_address(const char *sockpath,
382 const char *bindto,
383 const char *port)
384{
385 SocketAddress *saddr;
386
387 saddr = g_new0(SocketAddress, 1);
388 if (sockpath) {
Eric Blake2d32add2015-10-26 16:34:55 -0600389 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
Eric Blake32bafa82016-03-17 16:48:37 -0600390 saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
391 saddr->u.q_unix.data->path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100392 } else {
Eric Blake03992932016-03-03 09:16:48 -0700393 InetSocketAddress *inet;
Eric Blake2d32add2015-10-26 16:34:55 -0600394 saddr->type = SOCKET_ADDRESS_KIND_INET;
Eric Blake32bafa82016-03-17 16:48:37 -0600395 inet = saddr->u.inet.data = g_new0(InetSocketAddress, 1);
Eric Blake03992932016-03-03 09:16:48 -0700396 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100397 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700398 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100399 } else {
Eric Blake03992932016-03-03 09:16:48 -0700400 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100401 }
402 }
403
404 return saddr;
405}
406
407
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000408static QemuOptsList file_opts = {
409 .name = "file",
410 .implied_opt_name = "file",
411 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
412 .desc = {
413 /* no elements => accept any params */
414 { /* end of list */ }
415 },
416};
417
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000418static QemuOptsList qemu_object_opts = {
419 .name = "object",
420 .implied_opt_name = "qom-type",
421 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
422 .desc = {
423 { }
424 },
425};
426
427
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000428
429static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
430{
431 Object *obj;
432 QCryptoTLSCreds *creds;
433
434 obj = object_resolve_path_component(
435 object_get_objects_root(), id);
436 if (!obj) {
437 error_setg(errp, "No TLS credentials with id '%s'",
438 id);
439 return NULL;
440 }
441 creds = (QCryptoTLSCreds *)
442 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
443 if (!creds) {
444 error_setg(errp, "Object with id '%s' is not TLS credentials",
445 id);
446 return NULL;
447 }
448
449 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
450 error_setg(errp,
451 "Expecting TLS credentials with a server endpoint");
452 return NULL;
453 }
454 object_ref(obj);
455 return creds;
456}
457
458
bellard7a5ca862008-05-27 21:13:40 +0000459int main(int argc, char **argv)
460{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200461 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000462 BlockDriverState *bs;
463 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200464 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000465 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000466 const char *bindto = "0.0.0.0";
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100467 const char *port = NULL;
468 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100469 char *device = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000470 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800471 QemuOpts *sn_opts = NULL;
472 const char *sn_id_or_name = NULL;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000473 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:x:";
bellard7a5ca862008-05-27 21:13:40 +0000474 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000475 { "help", no_argument, NULL, 'h' },
476 { "version", no_argument, NULL, 'V' },
477 { "bind", required_argument, NULL, 'b' },
478 { "port", required_argument, NULL, 'p' },
479 { "socket", required_argument, NULL, 'k' },
480 { "offset", required_argument, NULL, 'o' },
481 { "read-only", no_argument, NULL, 'r' },
482 { "partition", required_argument, NULL, 'P' },
483 { "connect", required_argument, NULL, 'c' },
484 { "disconnect", no_argument, NULL, 'd' },
485 { "snapshot", no_argument, NULL, 's' },
486 { "load-snapshot", required_argument, NULL, 'l' },
487 { "nocache", no_argument, NULL, 'n' },
488 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
489 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
490 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
491 { "detect-zeroes", required_argument, NULL,
492 QEMU_NBD_OPT_DETECT_ZEROES },
493 { "shared", required_argument, NULL, 'e' },
494 { "format", required_argument, NULL, 'f' },
495 { "persistent", no_argument, NULL, 't' },
496 { "verbose", no_argument, NULL, 'v' },
497 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
498 { "export-name", required_argument, NULL, 'x' },
499 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
500 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Blue Swirl660f11b2009-07-31 21:16:51 +0000501 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000502 };
503 int ch;
504 int opt_ind = 0;
bellard7a5ca862008-05-27 21:13:40 +0000505 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200506 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000507 int partition = -1;
Max Reitz4fbec262015-02-05 13:58:19 -0500508 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200509 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100510 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200511 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100512 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000513 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200514 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200515 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500516 QDict *options = NULL;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000517 const char *export_name = NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000518 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000519 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100520 bool writethrough = true;
bellard7a5ca862008-05-27 21:13:40 +0000521
Paolo Bonzinia517e882011-11-04 15:51:21 +0100522 /* The client thread uses SIGTERM to interrupt the server. A signal
523 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
524 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100525 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100526 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
527 sa_sigterm.sa_handler = termsig_handler;
528 sigaction(SIGTERM, &sa_sigterm, NULL);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100529
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300530 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100531
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000532 module_call_init(MODULE_INIT_QOM);
533 qemu_add_opts(&qemu_object_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800534 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100535
bellard7a5ca862008-05-27 21:13:40 +0000536 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
537 switch (ch) {
538 case 's':
ths2f726482008-07-03 11:47:46 +0000539 flags |= BDRV_O_SNAPSHOT;
540 break;
541 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200542 optarg = (char *) "none";
543 /* fallthrough */
544 case QEMU_NBD_OPT_CACHE:
545 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100546 error_report("-n and --cache can only be specified once");
547 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200548 }
549 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100550 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100551 error_report("Invalid cache mode `%s'", optarg);
552 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200553 }
bellard7a5ca862008-05-27 21:13:40 +0000554 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200555 case QEMU_NBD_OPT_AIO:
556 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100557 error_report("--aio can only be specified once");
558 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200559 }
560 seen_aio = true;
561 if (!strcmp(optarg, "native")) {
562 flags |= BDRV_O_NATIVE_AIO;
563 } else if (!strcmp(optarg, "threads")) {
564 /* this is the default */
565 } else {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100566 error_report("invalid aio mode `%s'", optarg);
567 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200568 }
569 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100570 case QEMU_NBD_OPT_DISCARD:
571 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100572 error_report("--discard can only be specified once");
573 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100574 }
575 seen_discard = true;
576 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100577 error_report("Invalid discard mode `%s'", optarg);
578 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100579 }
580 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200581 case QEMU_NBD_OPT_DETECT_ZEROES:
582 detect_zeroes =
583 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
584 optarg,
Eric Blake7fb1cf12015-11-18 01:52:57 -0700585 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Peter Lievenb3838a42014-08-13 19:20:18 +0200586 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
587 &local_err);
588 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100589 error_reportf_err(local_err,
590 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100591 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200592 }
593 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
594 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100595 error_report("setting detect-zeroes to unmap is not allowed "
596 "without setting discard operation to unmap");
597 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200598 }
599 break;
bellard7a5ca862008-05-27 21:13:40 +0000600 case 'b':
601 bindto = optarg;
602 break;
603 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100604 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000605 break;
606 case 'o':
607 dev_offset = strtoll (optarg, &end, 0);
608 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100609 error_report("Invalid offset `%s'", optarg);
610 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000611 }
612 if (dev_offset < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100613 error_report("Offset must be positive `%s'", optarg);
614 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000615 }
616 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800617 case 'l':
618 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100619 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
620 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800621 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100622 error_report("Failed in parsing snapshot param `%s'",
623 optarg);
624 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800625 }
626 } else {
627 sn_id_or_name = optarg;
628 }
629 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000630 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200631 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200632 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000633 break;
634 case 'P':
635 partition = strtol(optarg, &end, 0);
Peter Lieven713cc672014-08-13 19:20:19 +0200636 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100637 error_report("Invalid partition `%s'", optarg);
638 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200639 }
640 if (partition < 1 || partition > 8) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100641 error_report("Invalid partition %d", partition);
642 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200643 }
bellard7a5ca862008-05-27 21:13:40 +0000644 break;
thscd831bd2008-07-03 10:23:51 +0000645 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100646 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200647 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100648 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100649 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200650 }
thscd831bd2008-07-03 10:23:51 +0000651 break;
652 case 'd':
653 disconnect = true;
654 break;
655 case 'c':
656 device = optarg;
657 break;
ths3b05a8e2008-07-03 12:45:02 +0000658 case 'e':
659 shared = strtol(optarg, &end, 0);
660 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100661 error_report("Invalid shared device number '%s'", optarg);
662 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000663 }
664 if (shared < 1) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100665 error_report("Shared device number must be greater than 0");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100666 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000667 }
668 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000669 case 'f':
670 fmt = optarg;
671 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200672 case 't':
673 persistent = 1;
674 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000675 case 'x':
676 export_name = optarg;
677 break;
bellard7a5ca862008-05-27 21:13:40 +0000678 case 'v':
679 verbose = 1;
680 break;
681 case 'V':
682 version(argv[0]);
683 exit(0);
684 break;
685 case 'h':
686 usage(argv[0]);
687 exit(0);
688 break;
689 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100690 error_report("Try `%s --help' for more information.", argv[0]);
691 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000692 case QEMU_NBD_OPT_OBJECT: {
693 QemuOpts *opts;
694 opts = qemu_opts_parse_noisily(&qemu_object_opts,
695 optarg, true);
696 if (!opts) {
697 exit(EXIT_FAILURE);
698 }
699 } break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000700 case QEMU_NBD_OPT_TLSCREDS:
701 tlscredsid = optarg;
702 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000703 case QEMU_NBD_OPT_IMAGE_OPTS:
704 imageOpts = true;
705 break;
bellard7a5ca862008-05-27 21:13:40 +0000706 }
707 }
708
709 if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100710 error_report("Invalid number of arguments");
711 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100712 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000713 }
714
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000715 if (qemu_opts_foreach(&qemu_object_opts,
716 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200717 NULL, NULL)) {
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000718 exit(EXIT_FAILURE);
719 }
720
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000721 if (tlscredsid) {
722 if (sockpath) {
723 error_report("TLS is only supported with IPv4/IPv6");
724 exit(EXIT_FAILURE);
725 }
726 if (device) {
727 error_report("TLS is not supported with a host device");
728 exit(EXIT_FAILURE);
729 }
730 if (!export_name) {
731 /* Set the default NBD protocol export name, since
732 * we *must* use new style protocol for TLS */
733 export_name = "";
734 }
735 tlscreds = nbd_get_tls_creds(tlscredsid, &local_err);
736 if (local_err) {
737 error_report("Failed to get TLS creds %s",
738 error_get_pretty(local_err));
739 exit(EXIT_FAILURE);
740 }
741 }
742
thscd831bd2008-07-03 10:23:51 +0000743 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000744 int nbdfd = open(argv[optind], O_RDWR);
745 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100746 error_report("Cannot open %s: %s", argv[optind],
747 strerror(errno));
748 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100749 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000750 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000751
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000752 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000753
754 printf("%s disconnected\n", argv[optind]);
755
Peter Lieven713cc672014-08-13 19:20:19 +0200756 return 0;
thscd831bd2008-07-03 10:23:51 +0000757 }
758
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100759 if (device && !verbose) {
760 int stderr_fd[2];
761 pid_t pid;
762 int ret;
763
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100764 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100765 error_report("Error setting up communication pipe: %s",
766 strerror(errno));
767 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100768 }
769
770 /* Now daemonize, but keep a communication channel open to
771 * print errors and exit with the proper status code.
772 */
773 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500774 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100775 error_report("Failed to fork: %s", strerror(errno));
776 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500777 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100778 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400779 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100780
781 /* Temporarily redirect stderr to the parent's pipe... */
782 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100783 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100784 error_report("Failed to daemonize: %s", strerror(errno));
785 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100786 }
787
788 /* ... close the descriptor we inherited and go on. */
789 close(stderr_fd[1]);
790 } else {
791 bool errors = false;
792 char *buf;
793
794 /* In the parent. Print error messages from the child until
795 * it closes the pipe.
796 */
797 close(stderr_fd[1]);
798 buf = g_malloc(1024);
799 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
800 errors = true;
801 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100802 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100803 exit(EXIT_FAILURE);
804 }
805 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100806 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100807 error_report("Cannot read from daemon: %s",
808 strerror(errno));
809 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100810 }
811
812 /* Usually the daemon should not print any message.
813 * Exit with zero status in that case.
814 */
815 exit(errors);
816 }
817 }
818
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100819 if (device != NULL && sockpath == NULL) {
820 sockpath = g_malloc(128);
821 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000822 }
823
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100824 saddr = nbd_build_socket_address(sockpath, bindto, port);
825
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300826 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100827 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300828 exit(EXIT_FAILURE);
829 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100830 bdrv_init();
831 atexit(bdrv_close_all);
832
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000833 srcpath = argv[optind];
834 if (imageOpts) {
835 QemuOpts *opts;
836 if (fmt) {
837 error_report("--image-opts and -f are mutually exclusive");
838 exit(EXIT_FAILURE);
839 }
840 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
841 if (!opts) {
842 qemu_opts_reset(&file_opts);
843 exit(EXIT_FAILURE);
844 }
845 options = qemu_opts_to_qdict(opts, NULL);
846 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +0100847 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000848 } else {
849 if (fmt) {
850 options = qdict_new();
851 qdict_put(options, "driver", qstring_from_str(fmt));
852 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100853 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000854 }
855
Max Reitz4fbec262015-02-05 13:58:19 -0500856 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100857 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
858 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100859 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100860 }
Max Reitz4fbec262015-02-05 13:58:19 -0500861 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100862
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100863 blk_set_enable_write_cache(blk, !writethrough);
864
Wenchao Xia8c116b02013-12-04 17:10:55 +0800865 if (sn_opts) {
866 ret = bdrv_snapshot_load_tmp(bs,
867 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
868 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
869 &local_err);
870 } else if (sn_id_or_name) {
871 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
872 &local_err);
873 }
874 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100875 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100876 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800877 }
878
Peter Lievenb3838a42014-08-13 19:20:18 +0200879 bs->detect_zeroes = detect_zeroes;
Max Reitz4c58e802014-11-18 12:21:19 +0100880 fd_size = blk_getlength(blk);
Max Reitz98f44bb2015-02-25 13:08:21 -0500881 if (fd_size < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100882 error_report("Failed to determine the image length: %s",
883 strerror(-fd_size));
884 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500885 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100886
Paolo Bonzini185b4332012-03-05 08:56:10 +0100887 if (partition != -1) {
Max Reitz4c58e802014-11-18 12:21:19 +0100888 ret = find_partition(blk, partition, &dev_offset, &fd_size);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100889 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100890 error_report("Could not find partition %d: %s", partition,
Markus Armbrustera4699e52015-12-18 16:35:10 +0100891 strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100892 exit(EXIT_FAILURE);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100893 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100894 }
895
Max Reitz98f44bb2015-02-25 13:08:21 -0500896 exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed,
897 &local_err);
898 if (!exp) {
Markus Armbruster4fffeb52015-12-18 16:35:05 +0100899 error_report_err(local_err);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100900 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500901 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000902 if (export_name) {
903 nbd_export_set_name(exp, export_name);
904 newproto = true;
905 }
ths3b05a8e2008-07-03 12:45:02 +0000906
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000907 server_ioc = qio_channel_socket_new();
908 if (qio_channel_socket_listen_sync(server_ioc, saddr, &local_err) < 0) {
909 object_unref(OBJECT(server_ioc));
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100910 error_report_err(local_err);
bellard7a5ca862008-05-27 21:13:40 +0000911 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200912 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100913
914 if (device) {
915 int ret;
916
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100917 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100918 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100919 error_report("Failed to create client thread: %s", strerror(ret));
920 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100921 }
922 } else {
923 /* Shut up GCC warnings. */
924 memset(&client_thread, 0, sizeof(client_thread));
925 }
926
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000927 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +0000928
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400929 /* now when the initialization is (almost) complete, chdir("/")
930 * to free any busy filesystems */
931 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100932 error_report("Could not chdir to root directory: %s",
933 strerror(errno));
934 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400935 }
936
Paolo Bonzini7860a382012-09-18 13:31:56 +0200937 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000938 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200939 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200940 if (state == TERMINATE) {
941 state = TERMINATING;
942 nbd_export_close(exp);
943 nbd_export_put(exp);
944 exp = NULL;
945 }
946 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000947
Markus Armbruster26f54e92014-10-07 13:59:04 +0200948 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100949 if (sockpath) {
950 unlink(sockpath);
951 }
bellard7a5ca862008-05-27 21:13:40 +0000952
Markus Armbrusterfbf28a42014-09-29 16:07:55 +0200953 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800954
Paolo Bonzinia517e882011-11-04 15:51:21 +0100955 if (device) {
956 void *ret;
957 pthread_join(client_thread, &ret);
958 exit(ret != NULL);
959 } else {
960 exit(EXIT_SUCCESS);
961 }
bellard7a5ca862008-05-27 21:13:40 +0000962}