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 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
21 * We automatically delete the drive when a device using it gets
22 * unplugged. Questionable feature, but we can't just drop it.
23 * Device models call blockdev_mark_auto_del() to schedule the
24 * automatic deletion, and generic qdev code calls blockdev_auto_del()
25 * when deletion is actually safe.
27 void blockdev_mark_auto_del(BlockDriverState
*bs
)
29 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
34 void blockdev_auto_del(BlockDriverState
*bs
)
36 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
38 if (dinfo
->auto_del
) {
43 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
50 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
53 opts
= qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
58 qemu_opt_set(opts
, "file", file
);
62 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
66 /* seek interface, bus and unit */
68 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
69 if (dinfo
->type
== type
&&
78 int drive_get_max_bus(BlockInterfaceType type
)
84 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
85 if(dinfo
->type
== type
&&
92 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
96 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
97 if (dinfo
->bdrv
== bs
) {
104 static void bdrv_format_print(void *opaque
, const char *name
)
106 fprintf(stderr
, " %s", name
);
109 void drive_uninit(DriveInfo
*dinfo
)
111 qemu_opts_del(dinfo
->opts
);
112 bdrv_delete(dinfo
->bdrv
);
113 QTAILQ_REMOVE(&drives
, dinfo
, next
);
117 static int parse_block_error_action(const char *buf
, int is_read
)
119 if (!strcmp(buf
, "ignore")) {
120 return BLOCK_ERR_IGNORE
;
121 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
122 return BLOCK_ERR_STOP_ENOSPC
;
123 } else if (!strcmp(buf
, "stop")) {
124 return BLOCK_ERR_STOP_ANY
;
125 } else if (!strcmp(buf
, "report")) {
126 return BLOCK_ERR_REPORT
;
128 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
129 buf
, is_read
? "read" : "write");
134 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
137 const char *file
= NULL
;
140 const char *mediastr
= "";
141 BlockInterfaceType type
;
142 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
144 int cyls
, heads
, secs
, translation
;
145 BlockDriver
*drv
= NULL
;
150 int on_read_error
, on_write_error
;
158 translation
= BIOS_ATA_TRANSLATION_AUTO
;
160 if (default_to_scsi
) {
162 max_devs
= MAX_SCSI_DEVS
;
163 pstrcpy(devname
, sizeof(devname
), "scsi");
166 max_devs
= MAX_IDE_DEVS
;
167 pstrcpy(devname
, sizeof(devname
), "ide");
171 /* extract parameters */
172 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
173 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
174 index
= qemu_opt_get_number(opts
, "index", -1);
176 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
177 heads
= qemu_opt_get_number(opts
, "heads", 0);
178 secs
= qemu_opt_get_number(opts
, "secs", 0);
180 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
181 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
183 file
= qemu_opt_get(opts
, "file");
184 serial
= qemu_opt_get(opts
, "serial");
186 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
187 pstrcpy(devname
, sizeof(devname
), buf
);
188 if (!strcmp(buf
, "ide")) {
190 max_devs
= MAX_IDE_DEVS
;
191 } else if (!strcmp(buf
, "scsi")) {
193 max_devs
= MAX_SCSI_DEVS
;
194 } else if (!strcmp(buf
, "floppy")) {
197 } else if (!strcmp(buf
, "pflash")) {
200 } else if (!strcmp(buf
, "mtd")) {
203 } else if (!strcmp(buf
, "sd")) {
206 } else if (!strcmp(buf
, "virtio")) {
209 } else if (!strcmp(buf
, "xen")) {
212 } else if (!strcmp(buf
, "none")) {
216 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
221 if (cyls
|| heads
|| secs
) {
222 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
223 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
226 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
227 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
230 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
231 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
236 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
239 "qemu: '%s' trans must be used with cyls,heads and secs\n",
243 if (!strcmp(buf
, "none"))
244 translation
= BIOS_ATA_TRANSLATION_NONE
;
245 else if (!strcmp(buf
, "lba"))
246 translation
= BIOS_ATA_TRANSLATION_LBA
;
247 else if (!strcmp(buf
, "auto"))
248 translation
= BIOS_ATA_TRANSLATION_AUTO
;
250 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
255 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
256 if (!strcmp(buf
, "disk")) {
258 } else if (!strcmp(buf
, "cdrom")) {
259 if (cyls
|| secs
|| heads
) {
261 "qemu: '%s' invalid physical CHS format\n", buf
);
266 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
271 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
272 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
273 bdrv_flags
|= BDRV_O_NOCACHE
;
274 } else if (!strcmp(buf
, "writeback")) {
275 bdrv_flags
|= BDRV_O_CACHE_WB
;
276 } else if (!strcmp(buf
, "unsafe")) {
277 bdrv_flags
|= BDRV_O_CACHE_WB
;
278 bdrv_flags
|= BDRV_O_NO_FLUSH
;
279 } else if (!strcmp(buf
, "writethrough")) {
280 /* this is the default */
282 fprintf(stderr
, "qemu: invalid cache option\n");
287 #ifdef CONFIG_LINUX_AIO
288 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
289 if (!strcmp(buf
, "native")) {
290 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
291 } else if (!strcmp(buf
, "threads")) {
292 /* this is the default */
294 fprintf(stderr
, "qemu: invalid aio option\n");
300 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
301 if (strcmp(buf
, "?") == 0) {
302 fprintf(stderr
, "qemu: Supported formats:");
303 bdrv_iterate_format(bdrv_format_print
, NULL
);
304 fprintf(stderr
, "\n");
307 drv
= bdrv_find_whitelisted_format(buf
);
309 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
314 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
315 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
316 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
317 fprintf(stderr
, "werror is not supported by this format\n");
321 on_write_error
= parse_block_error_action(buf
, 0);
322 if (on_write_error
< 0) {
327 on_read_error
= BLOCK_ERR_REPORT
;
328 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
329 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
330 fprintf(stderr
, "rerror is not supported by this format\n");
334 on_read_error
= parse_block_error_action(buf
, 1);
335 if (on_read_error
< 0) {
340 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
341 if (type
!= IF_VIRTIO
) {
342 fprintf(stderr
, "addr is not supported\n");
347 /* compute bus and unit according index */
350 if (bus_id
!= 0 || unit_id
!= -1) {
352 "qemu: index cannot be used with bus and unit\n");
360 unit_id
= index
% max_devs
;
361 bus_id
= index
/ max_devs
;
365 /* if user doesn't specify a unit_id,
366 * try to find the first free
371 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
373 if (max_devs
&& unit_id
>= max_devs
) {
382 if (max_devs
&& unit_id
>= max_devs
) {
383 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
384 unit_id
, max_devs
- 1);
389 * ignore multiple definitions
392 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
399 dinfo
= qemu_mallocz(sizeof(*dinfo
));
400 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
401 dinfo
->id
= qemu_strdup(buf
);
403 /* no id supplied -> create one */
404 dinfo
->id
= qemu_mallocz(32);
405 if (type
== IF_IDE
|| type
== IF_SCSI
)
406 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
408 snprintf(dinfo
->id
, 32, "%s%i%s%i",
409 devname
, bus_id
, mediastr
, unit_id
);
411 snprintf(dinfo
->id
, 32, "%s%s%i",
412 devname
, mediastr
, unit_id
);
414 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
415 dinfo
->devaddr
= devaddr
;
418 dinfo
->unit
= unit_id
;
421 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
422 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
424 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
434 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
435 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
439 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
444 /* FIXME: This isn't really a floppy, but it's a reasonable
447 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
453 /* add virtio block device */
454 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
455 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
456 qemu_opt_set(opts
, "drive", dinfo
->id
);
458 qemu_opt_set(opts
, "addr", devaddr
);
463 if (!file
|| !*file
) {
468 /* always use cache=unsafe with snapshot */
469 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
470 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
473 if (media
== MEDIA_CDROM
) {
474 /* CDROM is fine for any interface, don't check. */
476 } else if (ro
== 1) {
477 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
478 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
483 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
485 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
487 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
488 file
, strerror(-ret
));
492 if (bdrv_key_required(dinfo
->bdrv
))
498 void do_commit(Monitor
*mon
, const QDict
*qdict
)
500 const char *device
= qdict_get_str(qdict
, "device");
501 BlockDriverState
*bs
;
503 if (!strcmp(device
, "all")) {
506 bs
= bdrv_find(device
);
508 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
515 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
518 if (!bdrv_is_removable(bs
)) {
519 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
520 bdrv_get_device_name(bs
));
523 if (bdrv_is_locked(bs
)) {
524 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
532 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
534 BlockDriverState
*bs
;
535 int force
= qdict_get_try_bool(qdict
, "force", 0);
536 const char *filename
= qdict_get_str(qdict
, "device");
538 bs
= bdrv_find(filename
);
540 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
543 return eject_device(mon
, bs
, force
);
546 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
549 BlockDriverState
*bs
;
552 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
554 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
558 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
559 if (err
== -EINVAL
) {
560 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
562 } else if (err
< 0) {
563 qerror_report(QERR_INVALID_PASSWORD
);
570 int do_change_block(Monitor
*mon
, const char *device
,
571 const char *filename
, const char *fmt
)
573 BlockDriverState
*bs
;
574 BlockDriver
*drv
= NULL
;
577 bs
= bdrv_find(device
);
579 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
583 drv
= bdrv_find_whitelisted_format(fmt
);
585 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
589 if (eject_device(mon
, bs
, 0) < 0) {
592 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
593 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
594 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
595 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
598 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);