root / qemu-nbd.c @ 5d794885
History | View | Annotate | Download (7.5 kB)
1 | 7a5ca864 | bellard | /*\
|
---|---|---|---|
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 | 7a5ca864 | bellard | * along with this program; if not, write to the Free Software
|
17 | 7a5ca864 | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 | 7a5ca864 | bellard | */
|
19 | 7a5ca864 | bellard | |
20 | 7a5ca864 | bellard | #include <qemu-common.h> |
21 | 7a5ca864 | bellard | #include "block_int.h" |
22 | 7a5ca864 | bellard | #include "nbd.h" |
23 | 7a5ca864 | bellard | |
24 | 7a5ca864 | bellard | #include <malloc.h> |
25 | 7a5ca864 | bellard | #include <stdarg.h> |
26 | 7a5ca864 | bellard | #include <stdio.h> |
27 | 7a5ca864 | bellard | #include <getopt.h> |
28 | 7a5ca864 | bellard | #include <err.h> |
29 | 7a5ca864 | bellard | #include <sys/socket.h> |
30 | 7a5ca864 | bellard | #include <netinet/in.h> |
31 | 7a5ca864 | bellard | #include <netinet/tcp.h> |
32 | 7a5ca864 | bellard | #include <arpa/inet.h> |
33 | 7a5ca864 | bellard | |
34 | 7a5ca864 | bellard | int verbose;
|
35 | 7a5ca864 | bellard | |
36 | 7a5ca864 | bellard | static void usage(const char *name) |
37 | 7a5ca864 | bellard | { |
38 | 7a5ca864 | bellard | printf( |
39 | 7a5ca864 | bellard | "Usage: %s [OPTIONS] FILE\n"
|
40 | 7a5ca864 | bellard | "QEMU Disk Network Block Device Server\n"
|
41 | 7a5ca864 | bellard | "\n"
|
42 | 7a5ca864 | bellard | " -p, --port=PORT port to listen on (default `1024')\n"
|
43 | 7a5ca864 | bellard | " -o, --offset=OFFSET offset into the image\n"
|
44 | 7a5ca864 | bellard | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
|
45 | 7a5ca864 | bellard | " -r, --read-only export read-only\n"
|
46 | 7a5ca864 | bellard | " -P, --partition=NUM only expose partition NUM\n"
|
47 | 7a5ca864 | bellard | " -v, --verbose display extra debugging information\n"
|
48 | 7a5ca864 | bellard | " -h, --help display this help and exit\n"
|
49 | 7a5ca864 | bellard | " -V, --version output version information and exit\n"
|
50 | 7a5ca864 | bellard | "\n"
|
51 | 7a5ca864 | bellard | "Report bugs to <anthony@codemonkey.ws>\n"
|
52 | 7a5ca864 | bellard | , name); |
53 | 7a5ca864 | bellard | } |
54 | 7a5ca864 | bellard | |
55 | 7a5ca864 | bellard | static void version(const char *name) |
56 | 7a5ca864 | bellard | { |
57 | 7a5ca864 | bellard | printf( |
58 | 7a5ca864 | bellard | "qemu-nbd version 0.0.1\n"
|
59 | 7a5ca864 | bellard | "Written by Anthony Liguori.\n"
|
60 | 7a5ca864 | bellard | "\n"
|
61 | 7a5ca864 | bellard | "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
|
62 | 7a5ca864 | bellard | "This is free software; see the source for copying conditions. There is NO\n"
|
63 | 7a5ca864 | bellard | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
|
64 | 7a5ca864 | bellard | ); |
65 | 7a5ca864 | bellard | } |
66 | 7a5ca864 | bellard | |
67 | 7a5ca864 | bellard | struct partition_record
|
68 | 7a5ca864 | bellard | { |
69 | 7a5ca864 | bellard | uint8_t bootable; |
70 | 7a5ca864 | bellard | uint8_t start_head; |
71 | 7a5ca864 | bellard | uint32_t start_cylinder; |
72 | 7a5ca864 | bellard | uint8_t start_sector; |
73 | 7a5ca864 | bellard | uint8_t system; |
74 | 7a5ca864 | bellard | uint8_t end_head; |
75 | 7a5ca864 | bellard | uint8_t end_cylinder; |
76 | 7a5ca864 | bellard | uint8_t end_sector; |
77 | 7a5ca864 | bellard | uint32_t start_sector_abs; |
78 | 7a5ca864 | bellard | uint32_t nb_sectors_abs; |
79 | 7a5ca864 | bellard | }; |
80 | 7a5ca864 | bellard | |
81 | 7a5ca864 | bellard | static void read_partition(uint8_t *p, struct partition_record *r) |
82 | 7a5ca864 | bellard | { |
83 | 7a5ca864 | bellard | r->bootable = p[0];
|
84 | 7a5ca864 | bellard | r->start_head = p[1];
|
85 | 7a5ca864 | bellard | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); |
86 | 7a5ca864 | bellard | r->start_sector = p[2] & 0x3f; |
87 | 7a5ca864 | bellard | r->system = p[4];
|
88 | 7a5ca864 | bellard | r->end_head = p[5];
|
89 | 7a5ca864 | bellard | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); |
90 | 7a5ca864 | bellard | r->end_sector = p[6] & 0x3f; |
91 | 7a5ca864 | bellard | r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; |
92 | 7a5ca864 | bellard | r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; |
93 | 7a5ca864 | bellard | } |
94 | 7a5ca864 | bellard | |
95 | 7a5ca864 | bellard | static int find_partition(BlockDriverState *bs, int partition, |
96 | 7a5ca864 | bellard | off_t *offset, off_t *size) |
97 | 7a5ca864 | bellard | { |
98 | 7a5ca864 | bellard | struct partition_record mbr[4]; |
99 | 7a5ca864 | bellard | uint8_t data[512];
|
100 | 7a5ca864 | bellard | int i;
|
101 | 7a5ca864 | bellard | int ext_partnum = 4; |
102 | 7a5ca864 | bellard | |
103 | 7a5ca864 | bellard | if (bdrv_read(bs, 0, data, 1)) |
104 | 7a5ca864 | bellard | errx(EINVAL, "error while reading");
|
105 | 7a5ca864 | bellard | |
106 | 7a5ca864 | bellard | if (data[510] != 0x55 || data[511] != 0xaa) { |
107 | 7a5ca864 | bellard | errno = -EINVAL; |
108 | 7a5ca864 | bellard | return -1; |
109 | 7a5ca864 | bellard | } |
110 | 7a5ca864 | bellard | |
111 | 7a5ca864 | bellard | for (i = 0; i < 4; i++) { |
112 | 7a5ca864 | bellard | read_partition(&data[446 + 16 * i], &mbr[i]); |
113 | 7a5ca864 | bellard | |
114 | 7a5ca864 | bellard | if (!mbr[i].nb_sectors_abs)
|
115 | 7a5ca864 | bellard | continue;
|
116 | 7a5ca864 | bellard | |
117 | 7a5ca864 | bellard | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { |
118 | 7a5ca864 | bellard | struct partition_record ext[4]; |
119 | 7a5ca864 | bellard | uint8_t data1[512];
|
120 | 7a5ca864 | bellard | int j;
|
121 | 7a5ca864 | bellard | |
122 | 7a5ca864 | bellard | if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) |
123 | 7a5ca864 | bellard | errx(EINVAL, "error while reading");
|
124 | 7a5ca864 | bellard | |
125 | 7a5ca864 | bellard | for (j = 0; j < 4; j++) { |
126 | 7a5ca864 | bellard | read_partition(&data1[446 + 16 * j], &ext[j]); |
127 | 7a5ca864 | bellard | if (!ext[j].nb_sectors_abs)
|
128 | 7a5ca864 | bellard | continue;
|
129 | 7a5ca864 | bellard | |
130 | 7a5ca864 | bellard | if ((ext_partnum + j + 1) == partition) { |
131 | 7a5ca864 | bellard | *offset = (uint64_t)ext[j].start_sector_abs << 9;
|
132 | 7a5ca864 | bellard | *size = (uint64_t)ext[j].nb_sectors_abs << 9;
|
133 | 7a5ca864 | bellard | return 0; |
134 | 7a5ca864 | bellard | } |
135 | 7a5ca864 | bellard | } |
136 | 7a5ca864 | bellard | ext_partnum += 4;
|
137 | 7a5ca864 | bellard | } else if ((i + 1) == partition) { |
138 | 7a5ca864 | bellard | *offset = (uint64_t)mbr[i].start_sector_abs << 9;
|
139 | 7a5ca864 | bellard | *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
|
140 | 7a5ca864 | bellard | return 0; |
141 | 7a5ca864 | bellard | } |
142 | 7a5ca864 | bellard | } |
143 | 7a5ca864 | bellard | |
144 | 7a5ca864 | bellard | errno = -ENOENT; |
145 | 7a5ca864 | bellard | return -1; |
146 | 7a5ca864 | bellard | } |
147 | 7a5ca864 | bellard | |
148 | 7a5ca864 | bellard | int main(int argc, char **argv) |
149 | 7a5ca864 | bellard | { |
150 | 7a5ca864 | bellard | BlockDriverState *bs; |
151 | 7a5ca864 | bellard | off_t dev_offset = 0;
|
152 | 7a5ca864 | bellard | off_t offset = 0;
|
153 | 7a5ca864 | bellard | bool readonly = false; |
154 | 7a5ca864 | bellard | const char *bindto = "0.0.0.0"; |
155 | 7a5ca864 | bellard | int port = 1024; |
156 | 7a5ca864 | bellard | int sock, csock;
|
157 | 7a5ca864 | bellard | struct sockaddr_in addr;
|
158 | 7a5ca864 | bellard | socklen_t addr_len = sizeof(addr);
|
159 | 7a5ca864 | bellard | off_t fd_size; |
160 | 7a5ca864 | bellard | const char *sopt = "hVbo:p:rsP:v"; |
161 | 7a5ca864 | bellard | struct option lopt[] = {
|
162 | 7a5ca864 | bellard | { "help", 0, 0, 'h' }, |
163 | 7a5ca864 | bellard | { "version", 0, 0, 'V' }, |
164 | 7a5ca864 | bellard | { "bind", 1, 0, 'b' }, |
165 | 7a5ca864 | bellard | { "port", 1, 0, 'p' }, |
166 | 7a5ca864 | bellard | { "offset", 1, 0, 'o' }, |
167 | 7a5ca864 | bellard | { "read-only", 0, 0, 'r' }, |
168 | 7a5ca864 | bellard | { "partition", 1, 0, 'P' }, |
169 | 7a5ca864 | bellard | { "snapshot", 0, 0, 's' }, |
170 | 7a5ca864 | bellard | { "verbose", 0, 0, 'v' }, |
171 | 7a5ca864 | bellard | }; |
172 | 7a5ca864 | bellard | int ch;
|
173 | 7a5ca864 | bellard | int opt_ind = 0; |
174 | 7a5ca864 | bellard | int li;
|
175 | 7a5ca864 | bellard | char *end;
|
176 | 7a5ca864 | bellard | bool snapshot = false; |
177 | 7a5ca864 | bellard | int partition = -1; |
178 | 7a5ca864 | bellard | |
179 | 7a5ca864 | bellard | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
180 | 7a5ca864 | bellard | switch (ch) {
|
181 | 7a5ca864 | bellard | case 's': |
182 | 7a5ca864 | bellard | snapshot = true;
|
183 | 7a5ca864 | bellard | break;
|
184 | 7a5ca864 | bellard | case 'b': |
185 | 7a5ca864 | bellard | bindto = optarg; |
186 | 7a5ca864 | bellard | break;
|
187 | 7a5ca864 | bellard | case 'p': |
188 | 7a5ca864 | bellard | li = strtol(optarg, &end, 0);
|
189 | 7a5ca864 | bellard | if (*end) {
|
190 | 7a5ca864 | bellard | errx(EINVAL, "Invalid port `%s'", optarg);
|
191 | 7a5ca864 | bellard | } |
192 | 7a5ca864 | bellard | if (li < 1 || li > 65535) { |
193 | 7a5ca864 | bellard | errx(EINVAL, "Port out of range `%s'", optarg);
|
194 | 7a5ca864 | bellard | } |
195 | 7a5ca864 | bellard | port = (uint16_t)li; |
196 | 7a5ca864 | bellard | break;
|
197 | 7a5ca864 | bellard | case 'o': |
198 | 7a5ca864 | bellard | dev_offset = strtoll (optarg, &end, 0);
|
199 | 7a5ca864 | bellard | if (*end) {
|
200 | 7a5ca864 | bellard | errx(EINVAL, "Invalid offset `%s'", optarg);
|
201 | 7a5ca864 | bellard | } |
202 | 7a5ca864 | bellard | if (dev_offset < 0) { |
203 | 7a5ca864 | bellard | errx(EINVAL, "Offset must be positive `%s'", optarg);
|
204 | 7a5ca864 | bellard | } |
205 | 7a5ca864 | bellard | break;
|
206 | 7a5ca864 | bellard | case 'r': |
207 | 7a5ca864 | bellard | readonly = true;
|
208 | 7a5ca864 | bellard | break;
|
209 | 7a5ca864 | bellard | case 'P': |
210 | 7a5ca864 | bellard | partition = strtol(optarg, &end, 0);
|
211 | 7a5ca864 | bellard | if (*end)
|
212 | 7a5ca864 | bellard | errx(EINVAL, "Invalid partition `%s'", optarg);
|
213 | 7a5ca864 | bellard | if (partition < 1 || partition > 8) |
214 | 7a5ca864 | bellard | errx(EINVAL, "Invalid partition %d", partition);
|
215 | 7a5ca864 | bellard | break;
|
216 | 7a5ca864 | bellard | case 'v': |
217 | 7a5ca864 | bellard | verbose = 1;
|
218 | 7a5ca864 | bellard | break;
|
219 | 7a5ca864 | bellard | case 'V': |
220 | 7a5ca864 | bellard | version(argv[0]);
|
221 | 7a5ca864 | bellard | exit(0);
|
222 | 7a5ca864 | bellard | break;
|
223 | 7a5ca864 | bellard | case 'h': |
224 | 7a5ca864 | bellard | usage(argv[0]);
|
225 | 7a5ca864 | bellard | exit(0);
|
226 | 7a5ca864 | bellard | break;
|
227 | 7a5ca864 | bellard | case '?': |
228 | 7a5ca864 | bellard | errx(EINVAL, "Try `%s --help' for more information.",
|
229 | 7a5ca864 | bellard | argv[0]);
|
230 | 7a5ca864 | bellard | } |
231 | 7a5ca864 | bellard | } |
232 | 7a5ca864 | bellard | |
233 | 7a5ca864 | bellard | if ((argc - optind) != 1) { |
234 | 7a5ca864 | bellard | errx(EINVAL, "Invalid number of argument.\n"
|
235 | 7a5ca864 | bellard | "Try `%s --help' for more information.",
|
236 | 7a5ca864 | bellard | argv[0]);
|
237 | 7a5ca864 | bellard | } |
238 | 7a5ca864 | bellard | |
239 | 7a5ca864 | bellard | bdrv_init(); |
240 | 7a5ca864 | bellard | |
241 | 7a5ca864 | bellard | bs = bdrv_new("hda");
|
242 | 7a5ca864 | bellard | if (bs == NULL) |
243 | 7a5ca864 | bellard | return 1; |
244 | 7a5ca864 | bellard | |
245 | 7a5ca864 | bellard | if (bdrv_open(bs, argv[optind], snapshot) == -1) |
246 | 7a5ca864 | bellard | return 1; |
247 | 7a5ca864 | bellard | |
248 | 7a5ca864 | bellard | fd_size = bs->total_sectors * 512;
|
249 | 7a5ca864 | bellard | |
250 | 7a5ca864 | bellard | if (partition != -1 && |
251 | 7a5ca864 | bellard | find_partition(bs, partition, &dev_offset, &fd_size)) |
252 | 7a5ca864 | bellard | errx(errno, "Could not find partition %d", partition);
|
253 | 7a5ca864 | bellard | |
254 | 7a5ca864 | bellard | sock = tcp_socket_incoming(bindto, port); |
255 | 7a5ca864 | bellard | if (sock == -1) |
256 | 7a5ca864 | bellard | return 1; |
257 | 7a5ca864 | bellard | |
258 | 7a5ca864 | bellard | csock = accept(sock, |
259 | 7a5ca864 | bellard | (struct sockaddr *)&addr,
|
260 | 7a5ca864 | bellard | &addr_len); |
261 | 7a5ca864 | bellard | if (csock == -1) |
262 | 7a5ca864 | bellard | return 1; |
263 | 7a5ca864 | bellard | |
264 | 7a5ca864 | bellard | /* new fd_size is calculated by find_partition */
|
265 | 7a5ca864 | bellard | if (nbd_negotiate(bs, csock, fd_size) == -1) |
266 | 7a5ca864 | bellard | return 1; |
267 | 7a5ca864 | bellard | |
268 | 7a5ca864 | bellard | while (nbd_trip(bs, csock, fd_size, dev_offset, &offset, readonly) == 0); |
269 | 7a5ca864 | bellard | |
270 | 7a5ca864 | bellard | close(csock); |
271 | 7a5ca864 | bellard | close(sock); |
272 | 7a5ca864 | bellard | bdrv_close(bs); |
273 | 7a5ca864 | bellard | |
274 | 7a5ca864 | bellard | return 0; |
275 | 7a5ca864 | bellard | } |