2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
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.
14 #include "qemu-option.h"
15 #include "qemu-config.h"
18 DriveInfo
*extboot_drive
= NULL
;
20 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
22 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
29 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
32 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
37 qemu_opt_set(opts
, "file", file
);
41 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
45 /* seek interface, bus and unit */
47 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
48 if (dinfo
->type
== type
&&
57 DriveInfo
*drive_get_by_id(const char *id
)
61 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
62 if (strcmp(id
, dinfo
->id
))
69 int drive_get_max_bus(BlockInterfaceType type
)
75 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
76 if(dinfo
->type
== type
&&
83 const char *drive_get_serial(BlockDriverState
*bdrv
)
87 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
88 if (dinfo
->bdrv
== bdrv
)
95 static void bdrv_format_print(void *opaque
, const char *name
)
97 fprintf(stderr
, " %s", name
);
100 void drive_uninit(DriveInfo
*dinfo
)
102 qemu_opts_del(dinfo
->opts
);
103 bdrv_delete(dinfo
->bdrv
);
104 QTAILQ_REMOVE(&drives
, dinfo
, next
);
108 static int parse_block_error_action(const char *buf
, int is_read
)
110 if (!strcmp(buf
, "ignore")) {
111 return BLOCK_ERR_IGNORE
;
112 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
113 return BLOCK_ERR_STOP_ENOSPC
;
114 } else if (!strcmp(buf
, "stop")) {
115 return BLOCK_ERR_STOP_ANY
;
116 } else if (!strcmp(buf
, "report")) {
117 return BLOCK_ERR_REPORT
;
119 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
120 buf
, is_read
? "read" : "write");
125 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
128 const char *file
= NULL
;
131 const char *mediastr
= "";
132 BlockInterfaceType type
;
133 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
135 int cyls
, heads
, secs
, translation
;
136 BlockDriver
*drv
= NULL
;
141 int on_read_error
, on_write_error
;
149 translation
= BIOS_ATA_TRANSLATION_AUTO
;
151 if (default_to_scsi
) {
153 max_devs
= MAX_SCSI_DEVS
;
154 pstrcpy(devname
, sizeof(devname
), "scsi");
157 max_devs
= MAX_IDE_DEVS
;
158 pstrcpy(devname
, sizeof(devname
), "ide");
162 /* extract parameters */
163 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
164 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
165 index
= qemu_opt_get_number(opts
, "index", -1);
167 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
168 heads
= qemu_opt_get_number(opts
, "heads", 0);
169 secs
= qemu_opt_get_number(opts
, "secs", 0);
171 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
172 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
174 file
= qemu_opt_get(opts
, "file");
175 serial
= qemu_opt_get(opts
, "serial");
177 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
178 pstrcpy(devname
, sizeof(devname
), buf
);
179 if (!strcmp(buf
, "ide")) {
181 max_devs
= MAX_IDE_DEVS
;
182 } else if (!strcmp(buf
, "scsi")) {
184 max_devs
= MAX_SCSI_DEVS
;
185 } else if (!strcmp(buf
, "floppy")) {
188 } else if (!strcmp(buf
, "pflash")) {
191 } else if (!strcmp(buf
, "mtd")) {
194 } else if (!strcmp(buf
, "sd")) {
197 } else if (!strcmp(buf
, "virtio")) {
200 } else if (!strcmp(buf
, "xen")) {
203 } else if (!strcmp(buf
, "none")) {
207 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
212 if (cyls
|| heads
|| secs
) {
213 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
214 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
217 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
218 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
221 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
222 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
227 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
230 "qemu: '%s' trans must be used with cyls,heads and secs\n",
234 if (!strcmp(buf
, "none"))
235 translation
= BIOS_ATA_TRANSLATION_NONE
;
236 else if (!strcmp(buf
, "lba"))
237 translation
= BIOS_ATA_TRANSLATION_LBA
;
238 else if (!strcmp(buf
, "auto"))
239 translation
= BIOS_ATA_TRANSLATION_AUTO
;
241 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
246 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
247 if (!strcmp(buf
, "disk")) {
249 } else if (!strcmp(buf
, "cdrom")) {
250 if (cyls
|| secs
|| heads
) {
252 "qemu: '%s' invalid physical CHS format\n", buf
);
257 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
262 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
263 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
264 bdrv_flags
|= BDRV_O_NOCACHE
;
265 } else if (!strcmp(buf
, "writeback")) {
266 bdrv_flags
|= BDRV_O_CACHE_WB
;
267 } else if (!strcmp(buf
, "unsafe")) {
268 bdrv_flags
|= BDRV_O_CACHE_WB
;
269 bdrv_flags
|= BDRV_O_NO_FLUSH
;
270 } else if (!strcmp(buf
, "writethrough")) {
271 /* this is the default */
273 fprintf(stderr
, "qemu: invalid cache option\n");
278 #ifdef CONFIG_LINUX_AIO
279 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
280 if (!strcmp(buf
, "native")) {
281 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
282 } else if (!strcmp(buf
, "threads")) {
283 /* this is the default */
285 fprintf(stderr
, "qemu: invalid aio option\n");
291 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
292 if (strcmp(buf
, "?") == 0) {
293 fprintf(stderr
, "qemu: Supported formats:");
294 bdrv_iterate_format(bdrv_format_print
, NULL
);
295 fprintf(stderr
, "\n");
298 drv
= bdrv_find_whitelisted_format(buf
);
300 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
305 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
306 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
307 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
308 fprintf(stderr
, "werror is no supported by this format\n");
312 on_write_error
= parse_block_error_action(buf
, 0);
313 if (on_write_error
< 0) {
318 on_read_error
= BLOCK_ERR_REPORT
;
319 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
320 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
321 fprintf(stderr
, "rerror is no supported by this format\n");
325 on_read_error
= parse_block_error_action(buf
, 1);
326 if (on_read_error
< 0) {
331 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
332 if (type
!= IF_VIRTIO
) {
333 fprintf(stderr
, "addr is not supported\n");
338 /* compute bus and unit according index */
341 if (bus_id
!= 0 || unit_id
!= -1) {
343 "qemu: index cannot be used with bus and unit\n");
351 unit_id
= index
% max_devs
;
352 bus_id
= index
/ max_devs
;
356 /* if user doesn't specify a unit_id,
357 * try to find the first free
362 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
364 if (max_devs
&& unit_id
>= max_devs
) {
373 if (max_devs
&& unit_id
>= max_devs
) {
374 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
375 unit_id
, max_devs
- 1);
380 * ignore multiple definitions
383 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
390 dinfo
= qemu_mallocz(sizeof(*dinfo
));
391 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
392 dinfo
->id
= qemu_strdup(buf
);
394 /* no id supplied -> create one */
395 dinfo
->id
= qemu_mallocz(32);
396 if (type
== IF_IDE
|| type
== IF_SCSI
)
397 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
399 snprintf(dinfo
->id
, 32, "%s%i%s%i",
400 devname
, bus_id
, mediastr
, unit_id
);
402 snprintf(dinfo
->id
, 32, "%s%s%i",
403 devname
, mediastr
, unit_id
);
405 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
406 dinfo
->devaddr
= devaddr
;
409 dinfo
->unit
= unit_id
;
412 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
413 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
415 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
425 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
426 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
430 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
435 /* FIXME: This isn't really a floppy, but it's a reasonable
438 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
444 /* add virtio block device */
445 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
446 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
447 qemu_opt_set(opts
, "drive", dinfo
->id
);
449 qemu_opt_set(opts
, "addr", devaddr
);
454 if (!file
|| !*file
) {
459 /* always use cache=unsafe with snapshot */
460 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
461 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
464 if (media
== MEDIA_CDROM
) {
465 /* CDROM is fine for any interface, don't check. */
467 } else if (ro
== 1) {
468 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
469 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
474 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
476 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
478 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
479 file
, strerror(-ret
));
483 if (bdrv_key_required(dinfo
->bdrv
))
489 void do_commit(Monitor
*mon
, const QDict
*qdict
)
491 const char *device
= qdict_get_str(qdict
, "device");
492 BlockDriverState
*bs
;
494 if (!strcmp(device
, "all")) {
497 bs
= bdrv_find(device
);
499 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
506 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
508 if (bdrv_is_inserted(bs
)) {
510 if (!bdrv_is_removable(bs
)) {
511 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
512 bdrv_get_device_name(bs
));
515 if (bdrv_is_locked(bs
)) {
516 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
525 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
527 BlockDriverState
*bs
;
528 int force
= qdict_get_int(qdict
, "force");
529 const char *filename
= qdict_get_str(qdict
, "device");
531 bs
= bdrv_find(filename
);
533 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
536 return eject_device(mon
, bs
, force
);
539 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
542 BlockDriverState
*bs
;
545 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
547 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
551 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
552 if (err
== -EINVAL
) {
553 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
555 } else if (err
< 0) {
556 qerror_report(QERR_INVALID_PASSWORD
);
563 int do_change_block(Monitor
*mon
, const char *device
,
564 const char *filename
, const char *fmt
)
566 BlockDriverState
*bs
;
567 BlockDriver
*drv
= NULL
;
570 bs
= bdrv_find(device
);
572 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
576 drv
= bdrv_find_whitelisted_format(fmt
);
578 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
582 if (eject_device(mon
, bs
, 0) < 0) {
585 bdrv_flags
= bdrv_get_type_hint(bs
) == BDRV_TYPE_CDROM
? 0 : BDRV_O_RDWR
;
586 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
587 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
590 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);