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