root / qemu-nbd.c @ 7e7f4a0e
History | View | Annotate | Download (16.8 kB)
1 | cd831bd7 | ths | /*
|
---|---|---|---|
2 | 7a5ca864 | bellard | * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
|
3 | 7a5ca864 | bellard | *
|
4 | 7a5ca864 | bellard | * Network Block Device
|
5 | 7a5ca864 | bellard | *
|
6 | 7a5ca864 | bellard | * This program is free software; you can redistribute it and/or modify
|
7 | 7a5ca864 | bellard | * it under the terms of the GNU General Public License as published by
|
8 | 7a5ca864 | bellard | * the Free Software Foundation; under version 2 of the License.
|
9 | 7a5ca864 | bellard | *
|
10 | 7a5ca864 | bellard | * This program is distributed in the hope that it will be useful,
|
11 | 7a5ca864 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | 7a5ca864 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 | 7a5ca864 | bellard | * GNU General Public License for more details.
|
14 | 7a5ca864 | bellard | *
|
15 | 7a5ca864 | bellard | * You should have received a copy of the GNU General Public License
|
16 | 8167ee88 | Blue Swirl | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
17 | 7a5ca864 | bellard | */
|
18 | 7a5ca864 | bellard | |
19 | 5a61cb60 | Stefan Weil | #include "qemu-common.h" |
20 | 38ceff04 | Paolo Bonzini | #include "block.h" |
21 | 7a5ca864 | bellard | #include "nbd.h" |
22 | 7a5ca864 | bellard | |
23 | 7a5ca864 | bellard | #include <stdarg.h> |
24 | 7a5ca864 | bellard | #include <stdio.h> |
25 | 7a5ca864 | bellard | #include <getopt.h> |
26 | 7a5ca864 | bellard | #include <err.h> |
27 | cd831bd7 | ths | #include <sys/types.h> |
28 | 7a5ca864 | bellard | #include <sys/socket.h> |
29 | 7a5ca864 | bellard | #include <netinet/in.h> |
30 | 7a5ca864 | bellard | #include <netinet/tcp.h> |
31 | 7a5ca864 | bellard | #include <arpa/inet.h> |
32 | cd831bd7 | ths | #include <signal.h> |
33 | 2bff4b6f | Blue Swirl | #include <libgen.h> |
34 | a517e88b | Paolo Bonzini | #include <pthread.h> |
35 | cd831bd7 | ths | |
36 | 39a5235c | Paolo Bonzini | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
37 | 39a5235c | Paolo Bonzini | #define QEMU_NBD_OPT_CACHE 1 |
38 | 39a5235c | Paolo Bonzini | #define QEMU_NBD_OPT_AIO 2 |
39 | 7a5ca864 | bellard | |
40 | af49bbbe | Paolo Bonzini | static NBDExport *exp;
|
41 | b1d8e52e | blueswir1 | static int verbose; |
42 | a517e88b | Paolo Bonzini | static char *srcpath; |
43 | a517e88b | Paolo Bonzini | static char *sockpath; |
44 | 7860a380 | Paolo Bonzini | static int persistent = 0; |
45 | 7860a380 | Paolo Bonzini | static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state; |
46 | a61c6782 | Paolo Bonzini | static int shared = 1; |
47 | a61c6782 | Paolo Bonzini | static int nb_fds; |
48 | 7a5ca864 | bellard | |
49 | 7a5ca864 | bellard | static void usage(const char *name) |
50 | 7a5ca864 | bellard | { |
51 | b033cd86 | Paolo Bonzini | (printf) ( |
52 | 7a5ca864 | bellard | "Usage: %s [OPTIONS] FILE\n"
|
53 | 7a5ca864 | bellard | "QEMU Disk Network Block Device Server\n"
|
54 | 7a5ca864 | bellard | "\n"
|
55 | b033cd86 | Paolo Bonzini | " -h, --help display this help and exit\n"
|
56 | b033cd86 | Paolo Bonzini | " -V, --version output version information and exit\n"
|
57 | b033cd86 | Paolo Bonzini | "\n"
|
58 | b033cd86 | Paolo Bonzini | "Connection properties:\n"
|
59 | c2e2872b | Laurent Vivier | " -p, --port=PORT port to listen on (default `%d')\n"
|
60 | 7a5ca864 | bellard | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
|
61 | cd831bd7 | ths | " -k, --socket=PATH path to the unix socket\n"
|
62 | cd831bd7 | ths | " (default '"SOCKET_PATH"')\n" |
63 | 3b05a8e9 | ths | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
|
64 | 75818250 | ths | " -t, --persistent don't exit on the last connection\n"
|
65 | 7a5ca864 | bellard | " -v, --verbose display extra debugging information\n"
|
66 | 7a5ca864 | bellard | "\n"
|
67 | b033cd86 | Paolo Bonzini | "Exposing part of the image:\n"
|
68 | b033cd86 | Paolo Bonzini | " -o, --offset=OFFSET offset into the image\n"
|
69 | b033cd86 | Paolo Bonzini | " -P, --partition=NUM only expose partition NUM\n"
|
70 | b033cd86 | Paolo Bonzini | "\n"
|
71 | b033cd86 | Paolo Bonzini | #ifdef __linux__
|
72 | b033cd86 | Paolo Bonzini | "Kernel NBD client support:\n"
|
73 | b033cd86 | Paolo Bonzini | " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
|
74 | b033cd86 | Paolo Bonzini | " -d, --disconnect disconnect the specified device\n"
|
75 | b033cd86 | Paolo Bonzini | "\n"
|
76 | b033cd86 | Paolo Bonzini | #endif
|
77 | b033cd86 | Paolo Bonzini | "\n"
|
78 | b033cd86 | Paolo Bonzini | "Block device options:\n"
|
79 | b033cd86 | Paolo Bonzini | " -r, --read-only export read-only\n"
|
80 | b033cd86 | Paolo Bonzini | " -s, --snapshot use snapshot file\n"
|
81 | b033cd86 | Paolo Bonzini | " -n, --nocache disable host cache\n"
|
82 | 39a5235c | Paolo Bonzini | " --cache=MODE set cache mode (none, writeback, ...)\n"
|
83 | 39a5235c | Paolo Bonzini | #ifdef CONFIG_LINUX_AIO
|
84 | 39a5235c | Paolo Bonzini | " --aio=MODE set AIO mode (native or threads)\n"
|
85 | 39a5235c | Paolo Bonzini | #endif
|
86 | b033cd86 | Paolo Bonzini | "\n"
|
87 | b033cd86 | Paolo Bonzini | "Report bugs to <qemu-devel@nongnu.org>\n"
|
88 | c2e2872b | Laurent Vivier | , name, NBD_DEFAULT_PORT, "DEVICE");
|
89 | 7a5ca864 | bellard | } |
90 | 7a5ca864 | bellard | |
91 | 7a5ca864 | bellard | static void version(const char *name) |
92 | 7a5ca864 | bellard | { |
93 | 7a5ca864 | bellard | printf( |
94 | 315bc7aa | ths | "%s version 0.0.1\n"
|
95 | 7a5ca864 | bellard | "Written by Anthony Liguori.\n"
|
96 | 7a5ca864 | bellard | "\n"
|
97 | 7a5ca864 | bellard | "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
|
98 | 7a5ca864 | bellard | "This is free software; see the source for copying conditions. There is NO\n"
|
99 | 7a5ca864 | bellard | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
|
100 | 315bc7aa | ths | , name); |
101 | 7a5ca864 | bellard | } |
102 | 7a5ca864 | bellard | |
103 | 7a5ca864 | bellard | struct partition_record
|
104 | 7a5ca864 | bellard | { |
105 | 7a5ca864 | bellard | uint8_t bootable; |
106 | 7a5ca864 | bellard | uint8_t start_head; |
107 | 7a5ca864 | bellard | uint32_t start_cylinder; |
108 | 7a5ca864 | bellard | uint8_t start_sector; |
109 | 7a5ca864 | bellard | uint8_t system; |
110 | 7a5ca864 | bellard | uint8_t end_head; |
111 | 7a5ca864 | bellard | uint8_t end_cylinder; |
112 | 7a5ca864 | bellard | uint8_t end_sector; |
113 | 7a5ca864 | bellard | uint32_t start_sector_abs; |
114 | 7a5ca864 | bellard | uint32_t nb_sectors_abs; |
115 | 7a5ca864 | bellard | }; |
116 | 7a5ca864 | bellard | |
117 | 7a5ca864 | bellard | static void read_partition(uint8_t *p, struct partition_record *r) |
118 | 7a5ca864 | bellard | { |
119 | 7a5ca864 | bellard | r->bootable = p[0];
|
120 | 7a5ca864 | bellard | r->start_head = p[1];
|
121 | 7a5ca864 | bellard | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); |
122 | 7a5ca864 | bellard | r->start_sector = p[2] & 0x3f; |
123 | 7a5ca864 | bellard | r->system = p[4];
|
124 | 7a5ca864 | bellard | r->end_head = p[5];
|
125 | 7a5ca864 | bellard | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); |
126 | 7a5ca864 | bellard | r->end_sector = p[6] & 0x3f; |
127 | 7a5ca864 | bellard | r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; |
128 | 7a5ca864 | bellard | r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; |
129 | 7a5ca864 | bellard | } |
130 | 7a5ca864 | bellard | |
131 | 7a5ca864 | bellard | static int find_partition(BlockDriverState *bs, int partition, |
132 | 7a5ca864 | bellard | off_t *offset, off_t *size) |
133 | 7a5ca864 | bellard | { |
134 | 7a5ca864 | bellard | struct partition_record mbr[4]; |
135 | 7a5ca864 | bellard | uint8_t data[512];
|
136 | 7a5ca864 | bellard | int i;
|
137 | 7a5ca864 | bellard | int ext_partnum = 4; |
138 | cb7cf0e3 | Ryota Ozaki | int ret;
|
139 | 7a5ca864 | bellard | |
140 | cb7cf0e3 | Ryota Ozaki | if ((ret = bdrv_read(bs, 0, data, 1)) < 0) { |
141 | cb7cf0e3 | Ryota Ozaki | errno = -ret; |
142 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "error while reading");
|
143 | cb7cf0e3 | Ryota Ozaki | } |
144 | 7a5ca864 | bellard | |
145 | 7a5ca864 | bellard | if (data[510] != 0x55 || data[511] != 0xaa) { |
146 | 185b4338 | Paolo Bonzini | return -EINVAL;
|
147 | 7a5ca864 | bellard | } |
148 | 7a5ca864 | bellard | |
149 | 7a5ca864 | bellard | for (i = 0; i < 4; i++) { |
150 | 7a5ca864 | bellard | read_partition(&data[446 + 16 * i], &mbr[i]); |
151 | 7a5ca864 | bellard | |
152 | 7a5ca864 | bellard | if (!mbr[i].nb_sectors_abs)
|
153 | 7a5ca864 | bellard | continue;
|
154 | 7a5ca864 | bellard | |
155 | 7a5ca864 | bellard | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { |
156 | 7a5ca864 | bellard | struct partition_record ext[4]; |
157 | 7a5ca864 | bellard | uint8_t data1[512];
|
158 | 7a5ca864 | bellard | int j;
|
159 | 7a5ca864 | bellard | |
160 | cb7cf0e3 | Ryota Ozaki | if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) { |
161 | cb7cf0e3 | Ryota Ozaki | errno = -ret; |
162 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "error while reading");
|
163 | cb7cf0e3 | Ryota Ozaki | } |
164 | 7a5ca864 | bellard | |
165 | 7a5ca864 | bellard | for (j = 0; j < 4; j++) { |
166 | 7a5ca864 | bellard | read_partition(&data1[446 + 16 * j], &ext[j]); |
167 | 7a5ca864 | bellard | if (!ext[j].nb_sectors_abs)
|
168 | 7a5ca864 | bellard | continue;
|
169 | 7a5ca864 | bellard | |
170 | 7a5ca864 | bellard | if ((ext_partnum + j + 1) == partition) { |
171 | 7a5ca864 | bellard | *offset = (uint64_t)ext[j].start_sector_abs << 9;
|
172 | 7a5ca864 | bellard | *size = (uint64_t)ext[j].nb_sectors_abs << 9;
|
173 | 7a5ca864 | bellard | return 0; |
174 | 7a5ca864 | bellard | } |
175 | 7a5ca864 | bellard | } |
176 | 7a5ca864 | bellard | ext_partnum += 4;
|
177 | 7a5ca864 | bellard | } else if ((i + 1) == partition) { |
178 | 7a5ca864 | bellard | *offset = (uint64_t)mbr[i].start_sector_abs << 9;
|
179 | 7a5ca864 | bellard | *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
|
180 | 7a5ca864 | bellard | return 0; |
181 | 7a5ca864 | bellard | } |
182 | 7a5ca864 | bellard | } |
183 | 7a5ca864 | bellard | |
184 | 185b4338 | Paolo Bonzini | return -ENOENT;
|
185 | 7a5ca864 | bellard | } |
186 | 7a5ca864 | bellard | |
187 | bb345110 | Paolo Bonzini | static void termsig_handler(int signum) |
188 | bb345110 | Paolo Bonzini | { |
189 | 7860a380 | Paolo Bonzini | state = TERMINATE; |
190 | a61c6782 | Paolo Bonzini | qemu_notify_event(); |
191 | bb345110 | Paolo Bonzini | } |
192 | bb345110 | Paolo Bonzini | |
193 | a517e88b | Paolo Bonzini | static void *show_parts(void *arg) |
194 | cd831bd7 | ths | { |
195 | a6ac2313 | Paolo Bonzini | char *device = arg;
|
196 | a517e88b | Paolo Bonzini | int nbd;
|
197 | a517e88b | Paolo Bonzini | |
198 | a517e88b | Paolo Bonzini | /* linux just needs an open() to trigger
|
199 | a517e88b | Paolo Bonzini | * the partition table update
|
200 | a517e88b | Paolo Bonzini | * but remember to load the module with max_part != 0 :
|
201 | a517e88b | Paolo Bonzini | * modprobe nbd max_part=63
|
202 | a517e88b | Paolo Bonzini | */
|
203 | a517e88b | Paolo Bonzini | nbd = open(device, O_RDWR); |
204 | fc19f8a0 | Paolo Bonzini | if (nbd >= 0) { |
205 | a517e88b | Paolo Bonzini | close(nbd); |
206 | a517e88b | Paolo Bonzini | } |
207 | a517e88b | Paolo Bonzini | return NULL; |
208 | a517e88b | Paolo Bonzini | } |
209 | cd831bd7 | ths | |
210 | a517e88b | Paolo Bonzini | static void *nbd_client_thread(void *arg) |
211 | a517e88b | Paolo Bonzini | { |
212 | a6ac2313 | Paolo Bonzini | char *device = arg;
|
213 | a517e88b | Paolo Bonzini | off_t size; |
214 | a517e88b | Paolo Bonzini | size_t blocksize; |
215 | a517e88b | Paolo Bonzini | uint32_t nbdflags; |
216 | a6ac2313 | Paolo Bonzini | int fd, sock;
|
217 | a517e88b | Paolo Bonzini | int ret;
|
218 | a517e88b | Paolo Bonzini | pthread_t show_parts_thread; |
219 | a517e88b | Paolo Bonzini | |
220 | dc10e8b3 | Stefan Hajnoczi | sock = unix_socket_outgoing(sockpath); |
221 | fc19f8a0 | Paolo Bonzini | if (sock < 0) { |
222 | dc10e8b3 | Stefan Hajnoczi | goto out;
|
223 | dc10e8b3 | Stefan Hajnoczi | } |
224 | a517e88b | Paolo Bonzini | |
225 | a517e88b | Paolo Bonzini | ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
|
226 | a517e88b | Paolo Bonzini | &size, &blocksize); |
227 | fc19f8a0 | Paolo Bonzini | if (ret < 0) { |
228 | a517e88b | Paolo Bonzini | goto out;
|
229 | a517e88b | Paolo Bonzini | } |
230 | a517e88b | Paolo Bonzini | |
231 | a6ac2313 | Paolo Bonzini | fd = open(device, O_RDWR); |
232 | fc19f8a0 | Paolo Bonzini | if (fd < 0) { |
233 | a6ac2313 | Paolo Bonzini | /* Linux-only, we can use %m in printf. */
|
234 | a6ac2313 | Paolo Bonzini | fprintf(stderr, "Failed to open %s: %m", device);
|
235 | a6ac2313 | Paolo Bonzini | goto out;
|
236 | a6ac2313 | Paolo Bonzini | } |
237 | a6ac2313 | Paolo Bonzini | |
238 | a517e88b | Paolo Bonzini | ret = nbd_init(fd, sock, nbdflags, size, blocksize); |
239 | fc19f8a0 | Paolo Bonzini | if (ret < 0) { |
240 | a517e88b | Paolo Bonzini | goto out;
|
241 | a517e88b | Paolo Bonzini | } |
242 | a517e88b | Paolo Bonzini | |
243 | a517e88b | Paolo Bonzini | /* update partition table */
|
244 | a6ac2313 | Paolo Bonzini | pthread_create(&show_parts_thread, NULL, show_parts, device);
|
245 | a517e88b | Paolo Bonzini | |
246 | c1f8fdc3 | Paolo Bonzini | if (verbose) {
|
247 | c1f8fdc3 | Paolo Bonzini | fprintf(stderr, "NBD device %s is now connected to %s\n",
|
248 | c1f8fdc3 | Paolo Bonzini | device, srcpath); |
249 | c1f8fdc3 | Paolo Bonzini | } else {
|
250 | c1f8fdc3 | Paolo Bonzini | /* Close stderr so that the qemu-nbd process exits. */
|
251 | c1f8fdc3 | Paolo Bonzini | dup2(STDOUT_FILENO, STDERR_FILENO); |
252 | c1f8fdc3 | Paolo Bonzini | } |
253 | a517e88b | Paolo Bonzini | |
254 | a517e88b | Paolo Bonzini | ret = nbd_client(fd); |
255 | a517e88b | Paolo Bonzini | if (ret) {
|
256 | a517e88b | Paolo Bonzini | goto out;
|
257 | cd831bd7 | ths | } |
258 | a517e88b | Paolo Bonzini | close(fd); |
259 | a517e88b | Paolo Bonzini | kill(getpid(), SIGTERM); |
260 | a517e88b | Paolo Bonzini | return (void *) EXIT_SUCCESS; |
261 | a517e88b | Paolo Bonzini | |
262 | a517e88b | Paolo Bonzini | out:
|
263 | a517e88b | Paolo Bonzini | kill(getpid(), SIGTERM); |
264 | a517e88b | Paolo Bonzini | return (void *) EXIT_FAILURE; |
265 | cd831bd7 | ths | } |
266 | cd831bd7 | ths | |
267 | a61c6782 | Paolo Bonzini | static int nbd_can_accept(void *opaque) |
268 | a61c6782 | Paolo Bonzini | { |
269 | a61c6782 | Paolo Bonzini | return nb_fds < shared;
|
270 | a61c6782 | Paolo Bonzini | } |
271 | a61c6782 | Paolo Bonzini | |
272 | 7860a380 | Paolo Bonzini | static void nbd_export_closed(NBDExport *exp) |
273 | 7860a380 | Paolo Bonzini | { |
274 | 7860a380 | Paolo Bonzini | assert(state == TERMINATING); |
275 | 7860a380 | Paolo Bonzini | state = TERMINATED; |
276 | 7860a380 | Paolo Bonzini | } |
277 | 7860a380 | Paolo Bonzini | |
278 | 1743b515 | Paolo Bonzini | static void nbd_client_closed(NBDClient *client) |
279 | a61c6782 | Paolo Bonzini | { |
280 | 1743b515 | Paolo Bonzini | nb_fds--; |
281 | 7860a380 | Paolo Bonzini | if (nb_fds == 0 && !persistent && state == RUNNING) { |
282 | 7860a380 | Paolo Bonzini | state = TERMINATE; |
283 | 7860a380 | Paolo Bonzini | } |
284 | 1743b515 | Paolo Bonzini | qemu_notify_event(); |
285 | 7860a380 | Paolo Bonzini | nbd_client_put(client); |
286 | a61c6782 | Paolo Bonzini | } |
287 | a61c6782 | Paolo Bonzini | |
288 | a61c6782 | Paolo Bonzini | static void nbd_accept(void *opaque) |
289 | a61c6782 | Paolo Bonzini | { |
290 | a61c6782 | Paolo Bonzini | int server_fd = (uintptr_t) opaque;
|
291 | a61c6782 | Paolo Bonzini | struct sockaddr_in addr;
|
292 | a61c6782 | Paolo Bonzini | socklen_t addr_len = sizeof(addr);
|
293 | a61c6782 | Paolo Bonzini | |
294 | a61c6782 | Paolo Bonzini | int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); |
295 | 7860a380 | Paolo Bonzini | if (state >= TERMINATE) {
|
296 | 7860a380 | Paolo Bonzini | close(fd); |
297 | 7860a380 | Paolo Bonzini | return;
|
298 | 7860a380 | Paolo Bonzini | } |
299 | 7860a380 | Paolo Bonzini | |
300 | fc19f8a0 | Paolo Bonzini | if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) { |
301 | a61c6782 | Paolo Bonzini | nb_fds++; |
302 | a61c6782 | Paolo Bonzini | } |
303 | a61c6782 | Paolo Bonzini | } |
304 | a61c6782 | Paolo Bonzini | |
305 | 7a5ca864 | bellard | int main(int argc, char **argv) |
306 | 7a5ca864 | bellard | { |
307 | 7a5ca864 | bellard | BlockDriverState *bs; |
308 | 7a5ca864 | bellard | off_t dev_offset = 0;
|
309 | b90fb4b8 | Paolo Bonzini | uint32_t nbdflags = 0;
|
310 | cd831bd7 | ths | bool disconnect = false; |
311 | 7a5ca864 | bellard | const char *bindto = "0.0.0.0"; |
312 | a6ac2313 | Paolo Bonzini | char *device = NULL; |
313 | c2e2872b | Laurent Vivier | int port = NBD_DEFAULT_PORT;
|
314 | 7a5ca864 | bellard | off_t fd_size; |
315 | d6aa671f | aliguori | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; |
316 | 7a5ca864 | bellard | struct option lopt[] = {
|
317 | 660f11be | Blue Swirl | { "help", 0, NULL, 'h' }, |
318 | 660f11be | Blue Swirl | { "version", 0, NULL, 'V' }, |
319 | 660f11be | Blue Swirl | { "bind", 1, NULL, 'b' }, |
320 | 660f11be | Blue Swirl | { "port", 1, NULL, 'p' }, |
321 | 660f11be | Blue Swirl | { "socket", 1, NULL, 'k' }, |
322 | 660f11be | Blue Swirl | { "offset", 1, NULL, 'o' }, |
323 | 660f11be | Blue Swirl | { "read-only", 0, NULL, 'r' }, |
324 | 660f11be | Blue Swirl | { "partition", 1, NULL, 'P' }, |
325 | 660f11be | Blue Swirl | { "connect", 1, NULL, 'c' }, |
326 | 660f11be | Blue Swirl | { "disconnect", 0, NULL, 'd' }, |
327 | 660f11be | Blue Swirl | { "snapshot", 0, NULL, 's' }, |
328 | 660f11be | Blue Swirl | { "nocache", 0, NULL, 'n' }, |
329 | 39a5235c | Paolo Bonzini | { "cache", 1, NULL, QEMU_NBD_OPT_CACHE }, |
330 | 39a5235c | Paolo Bonzini | #ifdef CONFIG_LINUX_AIO
|
331 | 39a5235c | Paolo Bonzini | { "aio", 1, NULL, QEMU_NBD_OPT_AIO }, |
332 | 39a5235c | Paolo Bonzini | #endif
|
333 | 660f11be | Blue Swirl | { "shared", 1, NULL, 'e' }, |
334 | 660f11be | Blue Swirl | { "persistent", 0, NULL, 't' }, |
335 | 660f11be | Blue Swirl | { "verbose", 0, NULL, 'v' }, |
336 | 660f11be | Blue Swirl | { NULL, 0, NULL, 0 } |
337 | 7a5ca864 | bellard | }; |
338 | 7a5ca864 | bellard | int ch;
|
339 | 7a5ca864 | bellard | int opt_ind = 0; |
340 | 7a5ca864 | bellard | int li;
|
341 | 7a5ca864 | bellard | char *end;
|
342 | f5edb014 | Naphtali Sprei | int flags = BDRV_O_RDWR;
|
343 | 7a5ca864 | bellard | int partition = -1; |
344 | cd831bd7 | ths | int ret;
|
345 | 3b05a8e9 | ths | int fd;
|
346 | 39a5235c | Paolo Bonzini | bool seen_cache = false; |
347 | 39a5235c | Paolo Bonzini | #ifdef CONFIG_LINUX_AIO
|
348 | 39a5235c | Paolo Bonzini | bool seen_aio = false; |
349 | 39a5235c | Paolo Bonzini | #endif
|
350 | a517e88b | Paolo Bonzini | pthread_t client_thread; |
351 | 7a5ca864 | bellard | |
352 | a517e88b | Paolo Bonzini | /* The client thread uses SIGTERM to interrupt the server. A signal
|
353 | a517e88b | Paolo Bonzini | * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
|
354 | a517e88b | Paolo Bonzini | */
|
355 | bb345110 | Paolo Bonzini | struct sigaction sa_sigterm;
|
356 | bb345110 | Paolo Bonzini | memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
357 | bb345110 | Paolo Bonzini | sa_sigterm.sa_handler = termsig_handler; |
358 | bb345110 | Paolo Bonzini | sigaction(SIGTERM, &sa_sigterm, NULL);
|
359 | bb345110 | Paolo Bonzini | |
360 | 7a5ca864 | bellard | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
361 | 7a5ca864 | bellard | switch (ch) {
|
362 | 7a5ca864 | bellard | case 's': |
363 | 2f726488 | ths | flags |= BDRV_O_SNAPSHOT; |
364 | 2f726488 | ths | break;
|
365 | 2f726488 | ths | case 'n': |
366 | 39a5235c | Paolo Bonzini | optarg = (char *) "none"; |
367 | 39a5235c | Paolo Bonzini | /* fallthrough */
|
368 | 39a5235c | Paolo Bonzini | case QEMU_NBD_OPT_CACHE:
|
369 | 39a5235c | Paolo Bonzini | if (seen_cache) {
|
370 | 39a5235c | Paolo Bonzini | errx(EXIT_FAILURE, "-n and --cache can only be specified once");
|
371 | 39a5235c | Paolo Bonzini | } |
372 | 39a5235c | Paolo Bonzini | seen_cache = true;
|
373 | 39a5235c | Paolo Bonzini | if (bdrv_parse_cache_flags(optarg, &flags) == -1) { |
374 | 39a5235c | Paolo Bonzini | errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
|
375 | 39a5235c | Paolo Bonzini | } |
376 | 7a5ca864 | bellard | break;
|
377 | 39a5235c | Paolo Bonzini | #ifdef CONFIG_LINUX_AIO
|
378 | 39a5235c | Paolo Bonzini | case QEMU_NBD_OPT_AIO:
|
379 | 39a5235c | Paolo Bonzini | if (seen_aio) {
|
380 | 39a5235c | Paolo Bonzini | errx(EXIT_FAILURE, "--aio can only be specified once");
|
381 | 39a5235c | Paolo Bonzini | } |
382 | 39a5235c | Paolo Bonzini | seen_aio = true;
|
383 | 39a5235c | Paolo Bonzini | if (!strcmp(optarg, "native")) { |
384 | 39a5235c | Paolo Bonzini | flags |= BDRV_O_NATIVE_AIO; |
385 | 39a5235c | Paolo Bonzini | } else if (!strcmp(optarg, "threads")) { |
386 | 39a5235c | Paolo Bonzini | /* this is the default */
|
387 | 39a5235c | Paolo Bonzini | } else {
|
388 | 39a5235c | Paolo Bonzini | errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
|
389 | 39a5235c | Paolo Bonzini | } |
390 | 39a5235c | Paolo Bonzini | break;
|
391 | 39a5235c | Paolo Bonzini | #endif
|
392 | 7a5ca864 | bellard | case 'b': |
393 | 7a5ca864 | bellard | bindto = optarg; |
394 | 7a5ca864 | bellard | break;
|
395 | 7a5ca864 | bellard | case 'p': |
396 | 7a5ca864 | bellard | li = strtol(optarg, &end, 0);
|
397 | 7a5ca864 | bellard | if (*end) {
|
398 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
|
399 | 7a5ca864 | bellard | } |
400 | 7a5ca864 | bellard | if (li < 1 || li > 65535) { |
401 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
|
402 | 7a5ca864 | bellard | } |
403 | 7a5ca864 | bellard | port = (uint16_t)li; |
404 | 7a5ca864 | bellard | break;
|
405 | 7a5ca864 | bellard | case 'o': |
406 | 7a5ca864 | bellard | dev_offset = strtoll (optarg, &end, 0);
|
407 | 7a5ca864 | bellard | if (*end) {
|
408 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
|
409 | 7a5ca864 | bellard | } |
410 | 7a5ca864 | bellard | if (dev_offset < 0) { |
411 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
|
412 | 7a5ca864 | bellard | } |
413 | 7a5ca864 | bellard | break;
|
414 | 7a5ca864 | bellard | case 'r': |
415 | b90fb4b8 | Paolo Bonzini | nbdflags |= NBD_FLAG_READ_ONLY; |
416 | 07108b29 | Naphtali Sprei | flags &= ~BDRV_O_RDWR; |
417 | 7a5ca864 | bellard | break;
|
418 | 7a5ca864 | bellard | case 'P': |
419 | 7a5ca864 | bellard | partition = strtol(optarg, &end, 0);
|
420 | 7a5ca864 | bellard | if (*end)
|
421 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
|
422 | 7a5ca864 | bellard | if (partition < 1 || partition > 8) |
423 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid partition %d", partition);
|
424 | 7a5ca864 | bellard | break;
|
425 | cd831bd7 | ths | case 'k': |
426 | b32f6c28 | Paolo Bonzini | sockpath = optarg; |
427 | b32f6c28 | Paolo Bonzini | if (sockpath[0] != '/') |
428 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "socket path must be absolute\n");
|
429 | cd831bd7 | ths | break;
|
430 | cd831bd7 | ths | case 'd': |
431 | cd831bd7 | ths | disconnect = true;
|
432 | cd831bd7 | ths | break;
|
433 | cd831bd7 | ths | case 'c': |
434 | cd831bd7 | ths | device = optarg; |
435 | cd831bd7 | ths | break;
|
436 | 3b05a8e9 | ths | case 'e': |
437 | 3b05a8e9 | ths | shared = strtol(optarg, &end, 0);
|
438 | 3b05a8e9 | ths | if (*end) {
|
439 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
|
440 | 3b05a8e9 | ths | } |
441 | 3b05a8e9 | ths | if (shared < 1) { |
442 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
|
443 | 3b05a8e9 | ths | } |
444 | 3b05a8e9 | ths | break;
|
445 | 75818250 | ths | case 't': |
446 | 75818250 | ths | persistent = 1;
|
447 | 75818250 | ths | break;
|
448 | 7a5ca864 | bellard | case 'v': |
449 | 7a5ca864 | bellard | verbose = 1;
|
450 | 7a5ca864 | bellard | break;
|
451 | 7a5ca864 | bellard | case 'V': |
452 | 7a5ca864 | bellard | version(argv[0]);
|
453 | 7a5ca864 | bellard | exit(0);
|
454 | 7a5ca864 | bellard | break;
|
455 | 7a5ca864 | bellard | case 'h': |
456 | 7a5ca864 | bellard | usage(argv[0]);
|
457 | 7a5ca864 | bellard | exit(0);
|
458 | 7a5ca864 | bellard | break;
|
459 | 7a5ca864 | bellard | case '?': |
460 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Try `%s --help' for more information.",
|
461 | 7a5ca864 | bellard | argv[0]);
|
462 | 7a5ca864 | bellard | } |
463 | 7a5ca864 | bellard | } |
464 | 7a5ca864 | bellard | |
465 | 7a5ca864 | bellard | if ((argc - optind) != 1) { |
466 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid number of argument.\n"
|
467 | 7a5ca864 | bellard | "Try `%s --help' for more information.",
|
468 | 7a5ca864 | bellard | argv[0]);
|
469 | 7a5ca864 | bellard | } |
470 | 7a5ca864 | bellard | |
471 | cd831bd7 | ths | if (disconnect) {
|
472 | cd831bd7 | ths | fd = open(argv[optind], O_RDWR); |
473 | fc19f8a0 | Paolo Bonzini | if (fd < 0) { |
474 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
|
475 | fc19f8a0 | Paolo Bonzini | } |
476 | cd831bd7 | ths | nbd_disconnect(fd); |
477 | cd831bd7 | ths | |
478 | cd831bd7 | ths | close(fd); |
479 | cd831bd7 | ths | |
480 | cd831bd7 | ths | printf("%s disconnected\n", argv[optind]);
|
481 | cd831bd7 | ths | |
482 | cd831bd7 | ths | return 0; |
483 | cd831bd7 | ths | } |
484 | cd831bd7 | ths | |
485 | c1f8fdc3 | Paolo Bonzini | if (device && !verbose) {
|
486 | c1f8fdc3 | Paolo Bonzini | int stderr_fd[2]; |
487 | c1f8fdc3 | Paolo Bonzini | pid_t pid; |
488 | c1f8fdc3 | Paolo Bonzini | int ret;
|
489 | c1f8fdc3 | Paolo Bonzini | |
490 | fc19f8a0 | Paolo Bonzini | if (qemu_pipe(stderr_fd) < 0) { |
491 | c1f8fdc3 | Paolo Bonzini | err(EXIT_FAILURE, "Error setting up communication pipe");
|
492 | c1f8fdc3 | Paolo Bonzini | } |
493 | c1f8fdc3 | Paolo Bonzini | |
494 | c1f8fdc3 | Paolo Bonzini | /* Now daemonize, but keep a communication channel open to
|
495 | c1f8fdc3 | Paolo Bonzini | * print errors and exit with the proper status code.
|
496 | c1f8fdc3 | Paolo Bonzini | */
|
497 | c1f8fdc3 | Paolo Bonzini | pid = fork(); |
498 | c1f8fdc3 | Paolo Bonzini | if (pid == 0) { |
499 | c1f8fdc3 | Paolo Bonzini | close(stderr_fd[0]);
|
500 | 9faf31b6 | Michael Tokarev | ret = qemu_daemon(1, 0); |
501 | c1f8fdc3 | Paolo Bonzini | |
502 | c1f8fdc3 | Paolo Bonzini | /* Temporarily redirect stderr to the parent's pipe... */
|
503 | c1f8fdc3 | Paolo Bonzini | dup2(stderr_fd[1], STDERR_FILENO);
|
504 | fc19f8a0 | Paolo Bonzini | if (ret < 0) { |
505 | c1f8fdc3 | Paolo Bonzini | err(EXIT_FAILURE, "Failed to daemonize");
|
506 | c1f8fdc3 | Paolo Bonzini | } |
507 | c1f8fdc3 | Paolo Bonzini | |
508 | c1f8fdc3 | Paolo Bonzini | /* ... close the descriptor we inherited and go on. */
|
509 | c1f8fdc3 | Paolo Bonzini | close(stderr_fd[1]);
|
510 | c1f8fdc3 | Paolo Bonzini | } else {
|
511 | c1f8fdc3 | Paolo Bonzini | bool errors = false; |
512 | c1f8fdc3 | Paolo Bonzini | char *buf;
|
513 | c1f8fdc3 | Paolo Bonzini | |
514 | c1f8fdc3 | Paolo Bonzini | /* In the parent. Print error messages from the child until
|
515 | c1f8fdc3 | Paolo Bonzini | * it closes the pipe.
|
516 | c1f8fdc3 | Paolo Bonzini | */
|
517 | c1f8fdc3 | Paolo Bonzini | close(stderr_fd[1]);
|
518 | c1f8fdc3 | Paolo Bonzini | buf = g_malloc(1024);
|
519 | c1f8fdc3 | Paolo Bonzini | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { |
520 | c1f8fdc3 | Paolo Bonzini | errors = true;
|
521 | c1f8fdc3 | Paolo Bonzini | ret = qemu_write_full(STDERR_FILENO, buf, ret); |
522 | fc19f8a0 | Paolo Bonzini | if (ret < 0) { |
523 | c1f8fdc3 | Paolo Bonzini | exit(EXIT_FAILURE); |
524 | c1f8fdc3 | Paolo Bonzini | } |
525 | c1f8fdc3 | Paolo Bonzini | } |
526 | fc19f8a0 | Paolo Bonzini | if (ret < 0) { |
527 | c1f8fdc3 | Paolo Bonzini | err(EXIT_FAILURE, "Cannot read from daemon");
|
528 | c1f8fdc3 | Paolo Bonzini | } |
529 | c1f8fdc3 | Paolo Bonzini | |
530 | c1f8fdc3 | Paolo Bonzini | /* Usually the daemon should not print any message.
|
531 | c1f8fdc3 | Paolo Bonzini | * Exit with zero status in that case.
|
532 | c1f8fdc3 | Paolo Bonzini | */
|
533 | c1f8fdc3 | Paolo Bonzini | exit(errors); |
534 | c1f8fdc3 | Paolo Bonzini | } |
535 | c1f8fdc3 | Paolo Bonzini | } |
536 | c1f8fdc3 | Paolo Bonzini | |
537 | a6ac2313 | Paolo Bonzini | if (device != NULL && sockpath == NULL) { |
538 | a6ac2313 | Paolo Bonzini | sockpath = g_malloc(128);
|
539 | a6ac2313 | Paolo Bonzini | snprintf(sockpath, 128, SOCKET_PATH, basename(device));
|
540 | cd831bd7 | ths | } |
541 | cd831bd7 | ths | |
542 | 7e7f4a0e | Paolo Bonzini | qemu_init_main_loop(); |
543 | 802ddc37 | Paolo Bonzini | bdrv_init(); |
544 | 802ddc37 | Paolo Bonzini | atexit(bdrv_close_all); |
545 | 802ddc37 | Paolo Bonzini | |
546 | 802ddc37 | Paolo Bonzini | bs = bdrv_new("hda");
|
547 | 802ddc37 | Paolo Bonzini | srcpath = argv[optind]; |
548 | 802ddc37 | Paolo Bonzini | if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) { |
549 | 802ddc37 | Paolo Bonzini | errno = -ret; |
550 | 802ddc37 | Paolo Bonzini | err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
|
551 | 802ddc37 | Paolo Bonzini | } |
552 | 802ddc37 | Paolo Bonzini | |
553 | 38ceff04 | Paolo Bonzini | fd_size = bdrv_getlength(bs); |
554 | 802ddc37 | Paolo Bonzini | |
555 | 185b4338 | Paolo Bonzini | if (partition != -1) { |
556 | 185b4338 | Paolo Bonzini | ret = find_partition(bs, partition, &dev_offset, &fd_size); |
557 | 185b4338 | Paolo Bonzini | if (ret < 0) { |
558 | 185b4338 | Paolo Bonzini | errno = -ret; |
559 | 185b4338 | Paolo Bonzini | err(EXIT_FAILURE, "Could not find partition %d", partition);
|
560 | 185b4338 | Paolo Bonzini | } |
561 | 802ddc37 | Paolo Bonzini | } |
562 | 802ddc37 | Paolo Bonzini | |
563 | 7860a380 | Paolo Bonzini | exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed); |
564 | 3b05a8e9 | ths | |
565 | b32f6c28 | Paolo Bonzini | if (sockpath) {
|
566 | a61c6782 | Paolo Bonzini | fd = unix_socket_incoming(sockpath); |
567 | cd831bd7 | ths | } else {
|
568 | a61c6782 | Paolo Bonzini | fd = tcp_socket_incoming(bindto, port); |
569 | cd831bd7 | ths | } |
570 | cd831bd7 | ths | |
571 | fc19f8a0 | Paolo Bonzini | if (fd < 0) { |
572 | 7a5ca864 | bellard | return 1; |
573 | a61c6782 | Paolo Bonzini | } |
574 | f1ef5555 | Paolo Bonzini | |
575 | f1ef5555 | Paolo Bonzini | if (device) {
|
576 | f1ef5555 | Paolo Bonzini | int ret;
|
577 | f1ef5555 | Paolo Bonzini | |
578 | a6ac2313 | Paolo Bonzini | ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
|
579 | f1ef5555 | Paolo Bonzini | if (ret != 0) { |
580 | f1ef5555 | Paolo Bonzini | errx(EXIT_FAILURE, "Failed to create client thread: %s",
|
581 | f1ef5555 | Paolo Bonzini | strerror(ret)); |
582 | f1ef5555 | Paolo Bonzini | } |
583 | f1ef5555 | Paolo Bonzini | } else {
|
584 | f1ef5555 | Paolo Bonzini | /* Shut up GCC warnings. */
|
585 | f1ef5555 | Paolo Bonzini | memset(&client_thread, 0, sizeof(client_thread)); |
586 | f1ef5555 | Paolo Bonzini | } |
587 | f1ef5555 | Paolo Bonzini | |
588 | a61c6782 | Paolo Bonzini | qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
|
589 | a61c6782 | Paolo Bonzini | (void *)(uintptr_t)fd);
|
590 | 7a5ca864 | bellard | |
591 | 9faf31b6 | Michael Tokarev | /* now when the initialization is (almost) complete, chdir("/")
|
592 | 9faf31b6 | Michael Tokarev | * to free any busy filesystems */
|
593 | 9faf31b6 | Michael Tokarev | if (chdir("/") < 0) { |
594 | 9faf31b6 | Michael Tokarev | err(EXIT_FAILURE, "Could not chdir to root directory");
|
595 | 9faf31b6 | Michael Tokarev | } |
596 | 9faf31b6 | Michael Tokarev | |
597 | 7860a380 | Paolo Bonzini | state = RUNNING; |
598 | 3b05a8e9 | ths | do {
|
599 | a61c6782 | Paolo Bonzini | main_loop_wait(false);
|
600 | 7860a380 | Paolo Bonzini | if (state == TERMINATE) {
|
601 | 7860a380 | Paolo Bonzini | state = TERMINATING; |
602 | 7860a380 | Paolo Bonzini | nbd_export_close(exp); |
603 | 7860a380 | Paolo Bonzini | nbd_export_put(exp); |
604 | 7860a380 | Paolo Bonzini | exp = NULL;
|
605 | 7860a380 | Paolo Bonzini | } |
606 | 7860a380 | Paolo Bonzini | } while (state != TERMINATED);
|
607 | 7a5ca864 | bellard | |
608 | a4aab7b4 | Paolo Bonzini | bdrv_close(bs); |
609 | b32f6c28 | Paolo Bonzini | if (sockpath) {
|
610 | b32f6c28 | Paolo Bonzini | unlink(sockpath); |
611 | b32f6c28 | Paolo Bonzini | } |
612 | 7a5ca864 | bellard | |
613 | a517e88b | Paolo Bonzini | if (device) {
|
614 | a517e88b | Paolo Bonzini | void *ret;
|
615 | a517e88b | Paolo Bonzini | pthread_join(client_thread, &ret); |
616 | a517e88b | Paolo Bonzini | exit(ret != NULL);
|
617 | a517e88b | Paolo Bonzini | } else {
|
618 | a517e88b | Paolo Bonzini | exit(EXIT_SUCCESS); |
619 | a517e88b | Paolo Bonzini | } |
620 | 7a5ca864 | bellard | } |