Add a parameter to disable host cache, by Laurent Vivier.
[qemu] / qemu-nbd.c
1 /*
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
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <qemu-common.h>
21 #include "block_int.h"
22 #include "nbd.h"
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <getopt.h>
27 #include <err.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/tcp.h>
32 #include <arpa/inet.h>
33 #include <signal.h>
34
35 #define SOCKET_PATH    "/var/lock/qemu-nbd-%s"
36
37 #define NBD_BUFFER_SIZE (1024*1024)
38
39 int verbose;
40
41 static void usage(const char *name)
42 {
43     printf(
44 "Usage: %s [OPTIONS] FILE\n"
45 "QEMU Disk Network Block Device Server\n"
46 "\n"
47 "  -p, --port=PORT      port to listen on (default `1024')\n"
48 "  -o, --offset=OFFSET  offset into the image\n"
49 "  -b, --bind=IFACE     interface to bind to (default `0.0.0.0')\n"
50 "  -k, --socket=PATH    path to the unix socket\n"
51 "                       (default '"SOCKET_PATH"')\n"
52 "  -r, --read-only      export read-only\n"
53 "  -P, --partition=NUM  only expose partition NUM\n"
54 "  -s, --snapshot       use snapshot file\n"
55 "  -n, --nocache        disable host cache\n"
56 "  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
57 "  -d, --disconnect     disconnect the specified device\n"
58 "  -v, --verbose        display extra debugging information\n"
59 "  -h, --help           display this help and exit\n"
60 "  -V, --version        output version information and exit\n"
61 "\n"
62 "Report bugs to <anthony@codemonkey.ws>\n"
63     , name, "DEVICE");
64 }
65
66 static void version(const char *name)
67 {
68     printf(
69 "qemu-nbd version 0.0.1\n"
70 "Written by Anthony Liguori.\n"
71 "\n"
72 "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
73 "This is free software; see the source for copying conditions.  There is NO\n"
74 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
75     );
76 }
77
78 struct partition_record
79 {
80     uint8_t bootable;
81     uint8_t start_head;
82     uint32_t start_cylinder;
83     uint8_t start_sector;
84     uint8_t system;
85     uint8_t end_head;
86     uint8_t end_cylinder;
87     uint8_t end_sector;
88     uint32_t start_sector_abs;
89     uint32_t nb_sectors_abs;
90 };
91
92 static void read_partition(uint8_t *p, struct partition_record *r)
93 {
94     r->bootable = p[0];
95     r->start_head = p[1];
96     r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
97     r->start_sector = p[2] & 0x3f;
98     r->system = p[4];
99     r->end_head = p[5];
100     r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
101     r->end_sector = p[6] & 0x3f;
102     r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
103     r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
104 }
105
106 static int find_partition(BlockDriverState *bs, int partition,
107                           off_t *offset, off_t *size)
108 {
109     struct partition_record mbr[4];
110     uint8_t data[512];
111     int i;
112     int ext_partnum = 4;
113
114     if (bdrv_read(bs, 0, data, 1))
115         errx(EINVAL, "error while reading");
116
117     if (data[510] != 0x55 || data[511] != 0xaa) {
118         errno = -EINVAL;
119         return -1;
120     }
121
122     for (i = 0; i < 4; i++) {
123         read_partition(&data[446 + 16 * i], &mbr[i]);
124
125         if (!mbr[i].nb_sectors_abs)
126             continue;
127
128         if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
129             struct partition_record ext[4];
130             uint8_t data1[512];
131             int j;
132
133             if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1))
134                 errx(EINVAL, "error while reading");
135
136             for (j = 0; j < 4; j++) {
137                 read_partition(&data1[446 + 16 * j], &ext[j]);
138                 if (!ext[j].nb_sectors_abs)
139                     continue;
140
141                 if ((ext_partnum + j + 1) == partition) {
142                     *offset = (uint64_t)ext[j].start_sector_abs << 9;
143                     *size = (uint64_t)ext[j].nb_sectors_abs << 9;
144                     return 0;
145                 }
146             }
147             ext_partnum += 4;
148         } else if ((i + 1) == partition) {
149             *offset = (uint64_t)mbr[i].start_sector_abs << 9;
150             *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
151             return 0;
152         }
153     }
154
155     errno = -ENOENT;
156     return -1;
157 }
158
159 static void show_parts(const char *device)
160 {
161     if (fork() == 0) {
162         int nbd;
163
164         /* linux just needs an open() to trigger
165          * the partition table update
166          * but remember to load the module with max_part != 0 :
167          *     modprobe nbd max_part=63
168          */
169         nbd = open(device, O_RDWR);
170         if (nbd != -1)
171               close(nbd);
172         exit(0);
173     }
174 }
175
176 int main(int argc, char **argv)
177 {
178     BlockDriverState *bs;
179     off_t dev_offset = 0;
180     off_t offset = 0;
181     bool readonly = false;
182     bool disconnect = false;
183     const char *bindto = "0.0.0.0";
184     int port = 1024;
185     int sock, csock;
186     struct sockaddr_in addr;
187     socklen_t addr_len = sizeof(addr);
188     off_t fd_size;
189     char *device = NULL;
190     char *socket = NULL;
191     char sockpath[128];
192     const char *sopt = "hVbo:p:rsnP:c:dvk:";
193     struct option lopt[] = {
194         { "help", 0, 0, 'h' },
195         { "version", 0, 0, 'V' },
196         { "bind", 1, 0, 'b' },
197         { "port", 1, 0, 'p' },
198         { "socket", 1, 0, 'k' },
199         { "offset", 1, 0, 'o' },
200         { "read-only", 0, 0, 'r' },
201         { "partition", 1, 0, 'P' },
202         { "connect", 1, 0, 'c' },
203         { "disconnect", 0, 0, 'd' },
204         { "snapshot", 0, 0, 's' },
205         { "nocache", 0, 0, 'n' },
206         { "verbose", 0, 0, 'v' },
207         { NULL, 0, 0, 0 }
208     };
209     int ch;
210     int opt_ind = 0;
211     int li;
212     char *end;
213     int flags = 0;
214     int partition = -1;
215     int fd;
216     int ret;
217     uint8_t *data;
218
219     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
220         switch (ch) {
221         case 's':
222             flags |= BDRV_O_SNAPSHOT;
223             break;
224         case 'n':
225             flags |= BDRV_O_DIRECT;
226             break;
227         case 'b':
228             bindto = optarg;
229             break;
230         case 'p':
231             li = strtol(optarg, &end, 0);
232             if (*end) {
233                 errx(EINVAL, "Invalid port `%s'", optarg);
234             }
235             if (li < 1 || li > 65535) {
236                 errx(EINVAL, "Port out of range `%s'", optarg);
237             }
238             port = (uint16_t)li;
239             break;
240         case 'o':
241                 dev_offset = strtoll (optarg, &end, 0);
242             if (*end) {
243                 errx(EINVAL, "Invalid offset `%s'", optarg);
244             }
245             if (dev_offset < 0) {
246                 errx(EINVAL, "Offset must be positive `%s'", optarg);
247             }
248             break;
249         case 'r':
250             readonly = true;
251             break;
252         case 'P':
253             partition = strtol(optarg, &end, 0);
254             if (*end)
255                 errx(EINVAL, "Invalid partition `%s'", optarg);
256             if (partition < 1 || partition > 8)
257                 errx(EINVAL, "Invalid partition %d", partition);
258             break;
259         case 'k':
260             socket = optarg;
261             if (socket[0] != '/')
262                 errx(EINVAL, "socket path must be absolute\n");
263             break;
264         case 'd':
265             disconnect = true;
266             break;
267         case 'c':
268             device = optarg;
269             break;
270         case 'v':
271             verbose = 1;
272             break;
273         case 'V':
274             version(argv[0]);
275             exit(0);
276             break;
277         case 'h':
278             usage(argv[0]);
279             exit(0);
280             break;
281         case '?':
282             errx(EINVAL, "Try `%s --help' for more information.",
283                  argv[0]);
284         }
285     }
286
287     if ((argc - optind) != 1) {
288         errx(EINVAL, "Invalid number of argument.\n"
289              "Try `%s --help' for more information.",
290              argv[0]);
291     }
292
293     if (disconnect) {
294         fd = open(argv[optind], O_RDWR);
295         if (fd == -1)
296             errx(errno, "Cannot open %s", argv[optind]);
297
298         nbd_disconnect(fd);
299
300         close(fd);
301
302         printf("%s disconnected\n", argv[optind]);
303
304         return 0;
305     }
306
307     bdrv_init();
308
309     bs = bdrv_new("hda");
310     if (bs == NULL)
311         return 1;
312
313     if (bdrv_open(bs, argv[optind], flags) == -1)
314         return 1;
315
316     fd_size = bs->total_sectors * 512;
317
318     if (partition != -1 &&
319         find_partition(bs, partition, &dev_offset, &fd_size))
320         errx(errno, "Could not find partition %d", partition);
321
322     if (device) {
323         pid_t pid;
324         if (!verbose)
325             daemon(0, 0);       /* detach client and server */
326
327         if (socket == NULL) {
328             sprintf(sockpath, SOCKET_PATH, basename(device));
329             socket = sockpath;
330         }
331
332         pid = fork();
333         if (pid < 0)
334             return 1;
335         if (pid != 0) {
336             off_t size;
337             size_t blocksize;
338
339             ret = 0;
340             bdrv_close(bs);
341
342             do {
343                 sock = unix_socket_outgoing(socket);
344                 if (sock == -1) {
345                     if (errno != ENOENT && errno != ECONNREFUSED)
346                         goto out;
347                     sleep(1);   /* wait children */
348                 }
349             } while (sock == -1);
350
351             fd = open(device, O_RDWR);
352             if (fd == -1) {
353                 ret = 1;
354                 goto out;
355             }
356
357             ret = nbd_receive_negotiate(sock, &size, &blocksize);
358             if (ret == -1) {
359                 ret = 1;
360                 goto out;
361             }
362
363             ret = nbd_init(fd, sock, size, blocksize);
364             if (ret == -1) {
365                 ret = 1;
366                 goto out;
367             }
368
369             printf("NBD device %s is now connected to file %s\n",
370                     device, argv[optind]);
371
372             /* update partition table */
373
374             show_parts(device);
375
376             nbd_client(fd, sock);
377             close(fd);
378  out:
379             kill(pid, SIGTERM);
380             unlink(socket);
381
382             return ret;
383         }
384         /* children */
385     }
386
387     if (socket) {
388         sock = unix_socket_incoming(socket);
389     } else {
390         sock = tcp_socket_incoming(bindto, port);
391     }
392
393     if (sock == -1)
394         return 1;
395
396     csock = accept(sock,
397                (struct sockaddr *)&addr,
398                &addr_len);
399     if (csock == -1)
400         return 1;
401
402     /* new fd_size is calculated by find_partition */
403     if (nbd_negotiate(bs, csock, fd_size) == -1)
404         return 1;
405
406     data = qemu_memalign(512, NBD_BUFFER_SIZE);
407     while (nbd_trip(bs, csock, fd_size, dev_offset, &offset, readonly,
408                     data, NBD_BUFFER_SIZE) == 0);
409     qemu_free(data);
410
411     close(csock);
412     close(sock);
413     bdrv_close(bs);
414     if (socket)
415         unlink(socket);
416
417     return 0;
418 }