root / blockdev.c @ 0c6f9c17
History | View | Annotate | Download (15.4 kB)
1 |
/*
|
---|---|
2 |
* QEMU host block devices
|
3 |
*
|
4 |
* Copyright (c) 2003-2008 Fabrice Bellard
|
5 |
*
|
6 |
* This work is licensed under the terms of the GNU GPL, version 2 or
|
7 |
* later. See the COPYING file in the top-level directory.
|
8 |
*/
|
9 |
|
10 |
#include "block.h" |
11 |
#include "blockdev.h" |
12 |
#include "monitor.h" |
13 |
#include "qerror.h" |
14 |
#include "qemu-option.h" |
15 |
#include "qemu-config.h" |
16 |
#include "sysemu.h" |
17 |
|
18 |
static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
|
19 |
|
20 |
QemuOpts *drive_add(const char *file, const char *fmt, ...) |
21 |
{ |
22 |
va_list ap; |
23 |
char optstr[1024]; |
24 |
QemuOpts *opts; |
25 |
|
26 |
va_start(ap, fmt); |
27 |
vsnprintf(optstr, sizeof(optstr), fmt, ap);
|
28 |
va_end(ap); |
29 |
|
30 |
opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
|
31 |
if (!opts) {
|
32 |
return NULL; |
33 |
} |
34 |
if (file)
|
35 |
qemu_opt_set(opts, "file", file);
|
36 |
return opts;
|
37 |
} |
38 |
|
39 |
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
40 |
{ |
41 |
DriveInfo *dinfo; |
42 |
|
43 |
/* seek interface, bus and unit */
|
44 |
|
45 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
46 |
if (dinfo->type == type &&
|
47 |
dinfo->bus == bus && |
48 |
dinfo->unit == unit) |
49 |
return dinfo;
|
50 |
} |
51 |
|
52 |
return NULL; |
53 |
} |
54 |
|
55 |
DriveInfo *drive_get_by_id(const char *id) |
56 |
{ |
57 |
DriveInfo *dinfo; |
58 |
|
59 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
60 |
if (strcmp(id, dinfo->id))
|
61 |
continue;
|
62 |
return dinfo;
|
63 |
} |
64 |
return NULL; |
65 |
} |
66 |
|
67 |
int drive_get_max_bus(BlockInterfaceType type)
|
68 |
{ |
69 |
int max_bus;
|
70 |
DriveInfo *dinfo; |
71 |
|
72 |
max_bus = -1;
|
73 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
74 |
if(dinfo->type == type &&
|
75 |
dinfo->bus > max_bus) |
76 |
max_bus = dinfo->bus; |
77 |
} |
78 |
return max_bus;
|
79 |
} |
80 |
|
81 |
static void bdrv_format_print(void *opaque, const char *name) |
82 |
{ |
83 |
fprintf(stderr, " %s", name);
|
84 |
} |
85 |
|
86 |
void drive_uninit(DriveInfo *dinfo)
|
87 |
{ |
88 |
qemu_opts_del(dinfo->opts); |
89 |
bdrv_delete(dinfo->bdrv); |
90 |
QTAILQ_REMOVE(&drives, dinfo, next); |
91 |
qemu_free(dinfo); |
92 |
} |
93 |
|
94 |
static int parse_block_error_action(const char *buf, int is_read) |
95 |
{ |
96 |
if (!strcmp(buf, "ignore")) { |
97 |
return BLOCK_ERR_IGNORE;
|
98 |
} else if (!is_read && !strcmp(buf, "enospc")) { |
99 |
return BLOCK_ERR_STOP_ENOSPC;
|
100 |
} else if (!strcmp(buf, "stop")) { |
101 |
return BLOCK_ERR_STOP_ANY;
|
102 |
} else if (!strcmp(buf, "report")) { |
103 |
return BLOCK_ERR_REPORT;
|
104 |
} else {
|
105 |
fprintf(stderr, "qemu: '%s' invalid %s error action\n",
|
106 |
buf, is_read ? "read" : "write"); |
107 |
return -1; |
108 |
} |
109 |
} |
110 |
|
111 |
DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error) |
112 |
{ |
113 |
const char *buf; |
114 |
const char *file = NULL; |
115 |
char devname[128]; |
116 |
const char *serial; |
117 |
const char *mediastr = ""; |
118 |
BlockInterfaceType type; |
119 |
enum { MEDIA_DISK, MEDIA_CDROM } media;
|
120 |
int bus_id, unit_id;
|
121 |
int cyls, heads, secs, translation;
|
122 |
BlockDriver *drv = NULL;
|
123 |
int max_devs;
|
124 |
int index;
|
125 |
int ro = 0; |
126 |
int bdrv_flags = 0; |
127 |
int on_read_error, on_write_error;
|
128 |
const char *devaddr; |
129 |
DriveInfo *dinfo; |
130 |
int snapshot = 0; |
131 |
int ret;
|
132 |
|
133 |
*fatal_error = 1;
|
134 |
|
135 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
136 |
|
137 |
if (default_to_scsi) {
|
138 |
type = IF_SCSI; |
139 |
max_devs = MAX_SCSI_DEVS; |
140 |
pstrcpy(devname, sizeof(devname), "scsi"); |
141 |
} else {
|
142 |
type = IF_IDE; |
143 |
max_devs = MAX_IDE_DEVS; |
144 |
pstrcpy(devname, sizeof(devname), "ide"); |
145 |
} |
146 |
media = MEDIA_DISK; |
147 |
|
148 |
/* extract parameters */
|
149 |
bus_id = qemu_opt_get_number(opts, "bus", 0); |
150 |
unit_id = qemu_opt_get_number(opts, "unit", -1); |
151 |
index = qemu_opt_get_number(opts, "index", -1); |
152 |
|
153 |
cyls = qemu_opt_get_number(opts, "cyls", 0); |
154 |
heads = qemu_opt_get_number(opts, "heads", 0); |
155 |
secs = qemu_opt_get_number(opts, "secs", 0); |
156 |
|
157 |
snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
158 |
ro = qemu_opt_get_bool(opts, "readonly", 0); |
159 |
|
160 |
file = qemu_opt_get(opts, "file");
|
161 |
serial = qemu_opt_get(opts, "serial");
|
162 |
|
163 |
if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
164 |
pstrcpy(devname, sizeof(devname), buf);
|
165 |
if (!strcmp(buf, "ide")) { |
166 |
type = IF_IDE; |
167 |
max_devs = MAX_IDE_DEVS; |
168 |
} else if (!strcmp(buf, "scsi")) { |
169 |
type = IF_SCSI; |
170 |
max_devs = MAX_SCSI_DEVS; |
171 |
} else if (!strcmp(buf, "floppy")) { |
172 |
type = IF_FLOPPY; |
173 |
max_devs = 0;
|
174 |
} else if (!strcmp(buf, "pflash")) { |
175 |
type = IF_PFLASH; |
176 |
max_devs = 0;
|
177 |
} else if (!strcmp(buf, "mtd")) { |
178 |
type = IF_MTD; |
179 |
max_devs = 0;
|
180 |
} else if (!strcmp(buf, "sd")) { |
181 |
type = IF_SD; |
182 |
max_devs = 0;
|
183 |
} else if (!strcmp(buf, "virtio")) { |
184 |
type = IF_VIRTIO; |
185 |
max_devs = 0;
|
186 |
} else if (!strcmp(buf, "xen")) { |
187 |
type = IF_XEN; |
188 |
max_devs = 0;
|
189 |
} else if (!strcmp(buf, "none")) { |
190 |
type = IF_NONE; |
191 |
max_devs = 0;
|
192 |
} else {
|
193 |
fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
|
194 |
return NULL; |
195 |
} |
196 |
} |
197 |
|
198 |
if (cyls || heads || secs) {
|
199 |
if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { |
200 |
fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
|
201 |
return NULL; |
202 |
} |
203 |
if (heads < 1 || (type == IF_IDE && heads > 16)) { |
204 |
fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
|
205 |
return NULL; |
206 |
} |
207 |
if (secs < 1 || (type == IF_IDE && secs > 63)) { |
208 |
fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
|
209 |
return NULL; |
210 |
} |
211 |
} |
212 |
|
213 |
if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
214 |
if (!cyls) {
|
215 |
fprintf(stderr, |
216 |
"qemu: '%s' trans must be used with cyls,heads and secs\n",
|
217 |
buf); |
218 |
return NULL; |
219 |
} |
220 |
if (!strcmp(buf, "none")) |
221 |
translation = BIOS_ATA_TRANSLATION_NONE; |
222 |
else if (!strcmp(buf, "lba")) |
223 |
translation = BIOS_ATA_TRANSLATION_LBA; |
224 |
else if (!strcmp(buf, "auto")) |
225 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
226 |
else {
|
227 |
fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
|
228 |
return NULL; |
229 |
} |
230 |
} |
231 |
|
232 |
if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
233 |
if (!strcmp(buf, "disk")) { |
234 |
media = MEDIA_DISK; |
235 |
} else if (!strcmp(buf, "cdrom")) { |
236 |
if (cyls || secs || heads) {
|
237 |
fprintf(stderr, |
238 |
"qemu: '%s' invalid physical CHS format\n", buf);
|
239 |
return NULL; |
240 |
} |
241 |
media = MEDIA_CDROM; |
242 |
} else {
|
243 |
fprintf(stderr, "qemu: '%s' invalid media\n", buf);
|
244 |
return NULL; |
245 |
} |
246 |
} |
247 |
|
248 |
if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
249 |
if (!strcmp(buf, "off") || !strcmp(buf, "none")) { |
250 |
bdrv_flags |= BDRV_O_NOCACHE; |
251 |
} else if (!strcmp(buf, "writeback")) { |
252 |
bdrv_flags |= BDRV_O_CACHE_WB; |
253 |
} else if (!strcmp(buf, "unsafe")) { |
254 |
bdrv_flags |= BDRV_O_CACHE_WB; |
255 |
bdrv_flags |= BDRV_O_NO_FLUSH; |
256 |
} else if (!strcmp(buf, "writethrough")) { |
257 |
/* this is the default */
|
258 |
} else {
|
259 |
fprintf(stderr, "qemu: invalid cache option\n");
|
260 |
return NULL; |
261 |
} |
262 |
} |
263 |
|
264 |
#ifdef CONFIG_LINUX_AIO
|
265 |
if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
266 |
if (!strcmp(buf, "native")) { |
267 |
bdrv_flags |= BDRV_O_NATIVE_AIO; |
268 |
} else if (!strcmp(buf, "threads")) { |
269 |
/* this is the default */
|
270 |
} else {
|
271 |
fprintf(stderr, "qemu: invalid aio option\n");
|
272 |
return NULL; |
273 |
} |
274 |
} |
275 |
#endif
|
276 |
|
277 |
if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
278 |
if (strcmp(buf, "?") == 0) { |
279 |
fprintf(stderr, "qemu: Supported formats:");
|
280 |
bdrv_iterate_format(bdrv_format_print, NULL);
|
281 |
fprintf(stderr, "\n");
|
282 |
return NULL; |
283 |
} |
284 |
drv = bdrv_find_whitelisted_format(buf); |
285 |
if (!drv) {
|
286 |
fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
287 |
return NULL; |
288 |
} |
289 |
} |
290 |
|
291 |
on_write_error = BLOCK_ERR_STOP_ENOSPC; |
292 |
if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
293 |
if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
|
294 |
fprintf(stderr, "werror is no supported by this format\n");
|
295 |
return NULL; |
296 |
} |
297 |
|
298 |
on_write_error = parse_block_error_action(buf, 0);
|
299 |
if (on_write_error < 0) { |
300 |
return NULL; |
301 |
} |
302 |
} |
303 |
|
304 |
on_read_error = BLOCK_ERR_REPORT; |
305 |
if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
306 |
if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
|
307 |
fprintf(stderr, "rerror is no supported by this format\n");
|
308 |
return NULL; |
309 |
} |
310 |
|
311 |
on_read_error = parse_block_error_action(buf, 1);
|
312 |
if (on_read_error < 0) { |
313 |
return NULL; |
314 |
} |
315 |
} |
316 |
|
317 |
if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
318 |
if (type != IF_VIRTIO) {
|
319 |
fprintf(stderr, "addr is not supported\n");
|
320 |
return NULL; |
321 |
} |
322 |
} |
323 |
|
324 |
/* compute bus and unit according index */
|
325 |
|
326 |
if (index != -1) { |
327 |
if (bus_id != 0 || unit_id != -1) { |
328 |
fprintf(stderr, |
329 |
"qemu: index cannot be used with bus and unit\n");
|
330 |
return NULL; |
331 |
} |
332 |
if (max_devs == 0) |
333 |
{ |
334 |
unit_id = index; |
335 |
bus_id = 0;
|
336 |
} else {
|
337 |
unit_id = index % max_devs; |
338 |
bus_id = index / max_devs; |
339 |
} |
340 |
} |
341 |
|
342 |
/* if user doesn't specify a unit_id,
|
343 |
* try to find the first free
|
344 |
*/
|
345 |
|
346 |
if (unit_id == -1) { |
347 |
unit_id = 0;
|
348 |
while (drive_get(type, bus_id, unit_id) != NULL) { |
349 |
unit_id++; |
350 |
if (max_devs && unit_id >= max_devs) {
|
351 |
unit_id -= max_devs; |
352 |
bus_id++; |
353 |
} |
354 |
} |
355 |
} |
356 |
|
357 |
/* check unit id */
|
358 |
|
359 |
if (max_devs && unit_id >= max_devs) {
|
360 |
fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
|
361 |
unit_id, max_devs - 1);
|
362 |
return NULL; |
363 |
} |
364 |
|
365 |
/*
|
366 |
* ignore multiple definitions
|
367 |
*/
|
368 |
|
369 |
if (drive_get(type, bus_id, unit_id) != NULL) { |
370 |
*fatal_error = 0;
|
371 |
return NULL; |
372 |
} |
373 |
|
374 |
/* init */
|
375 |
|
376 |
dinfo = qemu_mallocz(sizeof(*dinfo));
|
377 |
if ((buf = qemu_opts_id(opts)) != NULL) { |
378 |
dinfo->id = qemu_strdup(buf); |
379 |
} else {
|
380 |
/* no id supplied -> create one */
|
381 |
dinfo->id = qemu_mallocz(32);
|
382 |
if (type == IF_IDE || type == IF_SCSI)
|
383 |
mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
384 |
if (max_devs)
|
385 |
snprintf(dinfo->id, 32, "%s%i%s%i", |
386 |
devname, bus_id, mediastr, unit_id); |
387 |
else
|
388 |
snprintf(dinfo->id, 32, "%s%s%i", |
389 |
devname, mediastr, unit_id); |
390 |
} |
391 |
dinfo->bdrv = bdrv_new(dinfo->id); |
392 |
dinfo->devaddr = devaddr; |
393 |
dinfo->type = type; |
394 |
dinfo->bus = bus_id; |
395 |
dinfo->unit = unit_id; |
396 |
dinfo->opts = opts; |
397 |
if (serial)
|
398 |
strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1); |
399 |
QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
400 |
|
401 |
bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); |
402 |
|
403 |
switch(type) {
|
404 |
case IF_IDE:
|
405 |
case IF_SCSI:
|
406 |
case IF_XEN:
|
407 |
case IF_NONE:
|
408 |
switch(media) {
|
409 |
case MEDIA_DISK:
|
410 |
if (cyls != 0) { |
411 |
bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
412 |
bdrv_set_translation_hint(dinfo->bdrv, translation); |
413 |
} |
414 |
break;
|
415 |
case MEDIA_CDROM:
|
416 |
bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
417 |
break;
|
418 |
} |
419 |
break;
|
420 |
case IF_SD:
|
421 |
/* FIXME: This isn't really a floppy, but it's a reasonable
|
422 |
approximation. */
|
423 |
case IF_FLOPPY:
|
424 |
bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
425 |
break;
|
426 |
case IF_PFLASH:
|
427 |
case IF_MTD:
|
428 |
break;
|
429 |
case IF_VIRTIO:
|
430 |
/* add virtio block device */
|
431 |
opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
432 |
qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
433 |
qemu_opt_set(opts, "drive", dinfo->id);
|
434 |
if (devaddr)
|
435 |
qemu_opt_set(opts, "addr", devaddr);
|
436 |
break;
|
437 |
case IF_COUNT:
|
438 |
abort(); |
439 |
} |
440 |
if (!file || !*file) {
|
441 |
*fatal_error = 0;
|
442 |
return NULL; |
443 |
} |
444 |
if (snapshot) {
|
445 |
/* always use cache=unsafe with snapshot */
|
446 |
bdrv_flags &= ~BDRV_O_CACHE_MASK; |
447 |
bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); |
448 |
} |
449 |
|
450 |
if (media == MEDIA_CDROM) {
|
451 |
/* CDROM is fine for any interface, don't check. */
|
452 |
ro = 1;
|
453 |
} else if (ro == 1) { |
454 |
if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
|
455 |
fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
|
456 |
return NULL; |
457 |
} |
458 |
} |
459 |
|
460 |
bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
|
461 |
|
462 |
ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv); |
463 |
if (ret < 0) { |
464 |
fprintf(stderr, "qemu: could not open disk image %s: %s\n",
|
465 |
file, strerror(-ret)); |
466 |
return NULL; |
467 |
} |
468 |
|
469 |
if (bdrv_key_required(dinfo->bdrv))
|
470 |
autostart = 0;
|
471 |
*fatal_error = 0;
|
472 |
return dinfo;
|
473 |
} |
474 |
|
475 |
void do_commit(Monitor *mon, const QDict *qdict) |
476 |
{ |
477 |
const char *device = qdict_get_str(qdict, "device"); |
478 |
BlockDriverState *bs; |
479 |
|
480 |
if (!strcmp(device, "all")) { |
481 |
bdrv_commit_all(); |
482 |
} else {
|
483 |
bs = bdrv_find(device); |
484 |
if (!bs) {
|
485 |
qerror_report(QERR_DEVICE_NOT_FOUND, device); |
486 |
return;
|
487 |
} |
488 |
bdrv_commit(bs); |
489 |
} |
490 |
} |
491 |
|
492 |
static int eject_device(Monitor *mon, BlockDriverState *bs, int force) |
493 |
{ |
494 |
if (!force) {
|
495 |
if (!bdrv_is_removable(bs)) {
|
496 |
qerror_report(QERR_DEVICE_NOT_REMOVABLE, |
497 |
bdrv_get_device_name(bs)); |
498 |
return -1; |
499 |
} |
500 |
if (bdrv_is_locked(bs)) {
|
501 |
qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); |
502 |
return -1; |
503 |
} |
504 |
} |
505 |
bdrv_close(bs); |
506 |
return 0; |
507 |
} |
508 |
|
509 |
int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data) |
510 |
{ |
511 |
BlockDriverState *bs; |
512 |
int force = qdict_get_int(qdict, "force"); |
513 |
const char *filename = qdict_get_str(qdict, "device"); |
514 |
|
515 |
bs = bdrv_find(filename); |
516 |
if (!bs) {
|
517 |
qerror_report(QERR_DEVICE_NOT_FOUND, filename); |
518 |
return -1; |
519 |
} |
520 |
return eject_device(mon, bs, force);
|
521 |
} |
522 |
|
523 |
int do_block_set_passwd(Monitor *mon, const QDict *qdict, |
524 |
QObject **ret_data) |
525 |
{ |
526 |
BlockDriverState *bs; |
527 |
int err;
|
528 |
|
529 |
bs = bdrv_find(qdict_get_str(qdict, "device"));
|
530 |
if (!bs) {
|
531 |
qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
|
532 |
return -1; |
533 |
} |
534 |
|
535 |
err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
|
536 |
if (err == -EINVAL) {
|
537 |
qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
538 |
return -1; |
539 |
} else if (err < 0) { |
540 |
qerror_report(QERR_INVALID_PASSWORD); |
541 |
return -1; |
542 |
} |
543 |
|
544 |
return 0; |
545 |
} |
546 |
|
547 |
int do_change_block(Monitor *mon, const char *device, |
548 |
const char *filename, const char *fmt) |
549 |
{ |
550 |
BlockDriverState *bs; |
551 |
BlockDriver *drv = NULL;
|
552 |
int bdrv_flags;
|
553 |
|
554 |
bs = bdrv_find(device); |
555 |
if (!bs) {
|
556 |
qerror_report(QERR_DEVICE_NOT_FOUND, device); |
557 |
return -1; |
558 |
} |
559 |
if (fmt) {
|
560 |
drv = bdrv_find_whitelisted_format(fmt); |
561 |
if (!drv) {
|
562 |
qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt); |
563 |
return -1; |
564 |
} |
565 |
} |
566 |
if (eject_device(mon, bs, 0) < 0) { |
567 |
return -1; |
568 |
} |
569 |
bdrv_flags = bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM ? 0 : BDRV_O_RDWR;
|
570 |
if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) { |
571 |
qerror_report(QERR_OPEN_FILE_FAILED, filename); |
572 |
return -1; |
573 |
} |
574 |
return monitor_read_bdrv_key_start(mon, bs, NULL, NULL); |
575 |
} |