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
);
23 * We automatically delete the drive when a device using it gets
24 * unplugged. Questionable feature, but we can't just drop it.
25 * Device models call blockdev_mark_auto_del() to schedule the
26 * automatic deletion, and generic qdev code calls blockdev_auto_del()
27 * when deletion is actually safe.
29 void blockdev_mark_auto_del(BlockDriverState
*bs
)
31 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
36 void blockdev_auto_del(BlockDriverState
*bs
)
38 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
40 if (dinfo
->auto_del
) {
45 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
52 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
55 opts
= qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
60 qemu_opt_set(opts
, "file", file
);
64 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
68 /* seek interface, bus and unit */
70 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
71 if (dinfo
->type
== type
&&
80 int drive_get_max_bus(BlockInterfaceType type
)
86 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
87 if(dinfo
->type
== type
&&
94 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
98 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
99 if (dinfo
->bdrv
== bs
) {
106 static void bdrv_format_print(void *opaque
, const char *name
)
108 fprintf(stderr
, " %s", name
);
111 void drive_uninit(DriveInfo
*dinfo
)
113 qemu_opts_del(dinfo
->opts
);
114 bdrv_delete(dinfo
->bdrv
);
115 QTAILQ_REMOVE(&drives
, dinfo
, next
);
119 static int parse_block_error_action(const char *buf
, int is_read
)
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
;
130 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
131 buf
, is_read
? "read" : "write");
136 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
139 const char *file
= NULL
;
142 const char *mediastr
= "";
143 BlockInterfaceType type
;
144 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
146 int cyls
, heads
, secs
, translation
;
147 BlockDriver
*drv
= NULL
;
152 int on_read_error
, on_write_error
;
161 translation
= BIOS_ATA_TRANSLATION_AUTO
;
163 if (default_to_scsi
) {
165 max_devs
= MAX_SCSI_DEVS
;
166 pstrcpy(devname
, sizeof(devname
), "scsi");
169 max_devs
= MAX_IDE_DEVS
;
170 pstrcpy(devname
, sizeof(devname
), "ide");
174 /* extract parameters */
175 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
176 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
177 index
= qemu_opt_get_number(opts
, "index", -1);
179 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
180 heads
= qemu_opt_get_number(opts
, "heads", 0);
181 secs
= qemu_opt_get_number(opts
, "secs", 0);
183 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
184 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
186 file
= qemu_opt_get(opts
, "file");
187 serial
= qemu_opt_get(opts
, "serial");
189 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
190 pstrcpy(devname
, sizeof(devname
), buf
);
191 if (!strcmp(buf
, "ide")) {
193 max_devs
= MAX_IDE_DEVS
;
194 } else if (!strcmp(buf
, "scsi")) {
196 max_devs
= MAX_SCSI_DEVS
;
197 } else if (!strcmp(buf
, "floppy")) {
200 } else if (!strcmp(buf
, "pflash")) {
203 } else if (!strcmp(buf
, "mtd")) {
206 } else if (!strcmp(buf
, "sd")) {
209 } else if (!strcmp(buf
, "virtio")) {
212 } else if (!strcmp(buf
, "xen")) {
215 } else if (!strcmp(buf
, "none")) {
219 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
224 if (cyls
|| heads
|| secs
) {
225 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
226 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
229 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
230 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
233 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
234 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
239 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
242 "qemu: '%s' trans must be used with cyls,heads and secs\n",
246 if (!strcmp(buf
, "none"))
247 translation
= BIOS_ATA_TRANSLATION_NONE
;
248 else if (!strcmp(buf
, "lba"))
249 translation
= BIOS_ATA_TRANSLATION_LBA
;
250 else if (!strcmp(buf
, "auto"))
251 translation
= BIOS_ATA_TRANSLATION_AUTO
;
253 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
258 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
259 if (!strcmp(buf
, "disk")) {
261 } else if (!strcmp(buf
, "cdrom")) {
262 if (cyls
|| secs
|| heads
) {
264 "qemu: '%s' invalid physical CHS format\n", buf
);
269 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
274 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
275 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
276 bdrv_flags
|= BDRV_O_NOCACHE
;
277 } else if (!strcmp(buf
, "writeback")) {
278 bdrv_flags
|= BDRV_O_CACHE_WB
;
279 } else if (!strcmp(buf
, "unsafe")) {
280 bdrv_flags
|= BDRV_O_CACHE_WB
;
281 bdrv_flags
|= BDRV_O_NO_FLUSH
;
282 } else if (!strcmp(buf
, "writethrough")) {
283 /* this is the default */
285 fprintf(stderr
, "qemu: invalid cache option\n");
290 #ifdef CONFIG_LINUX_AIO
291 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
292 if (!strcmp(buf
, "native")) {
293 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
294 } else if (!strcmp(buf
, "threads")) {
295 /* this is the default */
297 fprintf(stderr
, "qemu: invalid aio option\n");
303 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
304 if (strcmp(buf
, "?") == 0) {
305 fprintf(stderr
, "qemu: Supported formats:");
306 bdrv_iterate_format(bdrv_format_print
, NULL
);
307 fprintf(stderr
, "\n");
310 drv
= bdrv_find_whitelisted_format(buf
);
312 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
317 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
318 if (is_extboot
&& extboot_drive
) {
319 fprintf(stderr
, "qemu: two bootable drives specified\n");
323 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
324 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
325 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
326 fprintf(stderr
, "werror is no supported by this format\n");
330 on_write_error
= parse_block_error_action(buf
, 0);
331 if (on_write_error
< 0) {
336 on_read_error
= BLOCK_ERR_REPORT
;
337 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
338 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
339 fprintf(stderr
, "rerror is no supported by this format\n");
343 on_read_error
= parse_block_error_action(buf
, 1);
344 if (on_read_error
< 0) {
349 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
350 if (type
!= IF_VIRTIO
) {
351 fprintf(stderr
, "addr is not supported\n");
356 /* compute bus and unit according index */
359 if (bus_id
!= 0 || unit_id
!= -1) {
361 "qemu: index cannot be used with bus and unit\n");
369 unit_id
= index
% max_devs
;
370 bus_id
= index
/ max_devs
;
374 /* if user doesn't specify a unit_id,
375 * try to find the first free
380 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
382 if (max_devs
&& unit_id
>= max_devs
) {
391 if (max_devs
&& unit_id
>= max_devs
) {
392 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
393 unit_id
, max_devs
- 1);
398 * ignore multiple definitions
401 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
408 dinfo
= qemu_mallocz(sizeof(*dinfo
));
409 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
410 dinfo
->id
= qemu_strdup(buf
);
412 /* no id supplied -> create one */
413 dinfo
->id
= qemu_mallocz(32);
414 if (type
== IF_IDE
|| type
== IF_SCSI
)
415 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
417 snprintf(dinfo
->id
, 32, "%s%i%s%i",
418 devname
, bus_id
, mediastr
, unit_id
);
420 snprintf(dinfo
->id
, 32, "%s%s%i",
421 devname
, mediastr
, unit_id
);
423 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
424 dinfo
->devaddr
= devaddr
;
427 dinfo
->unit
= unit_id
;
430 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
431 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
434 extboot_drive
= dinfo
;
437 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
447 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
448 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
452 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
457 /* FIXME: This isn't really a floppy, but it's a reasonable
460 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
466 /* add virtio block device */
467 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
468 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
469 qemu_opt_set(opts
, "drive", dinfo
->id
);
471 qemu_opt_set(opts
, "addr", devaddr
);
476 if (!file
|| !*file
) {
481 /* always use cache=unsafe with snapshot */
482 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
483 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
486 if (media
== MEDIA_CDROM
) {
487 /* CDROM is fine for any interface, don't check. */
489 } else if (ro
== 1) {
490 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
491 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
496 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
498 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
500 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
501 file
, strerror(-ret
));
505 if (bdrv_key_required(dinfo
->bdrv
))
511 void do_commit(Monitor
*mon
, const QDict
*qdict
)
513 const char *device
= qdict_get_str(qdict
, "device");
514 BlockDriverState
*bs
;
516 if (!strcmp(device
, "all")) {
519 bs
= bdrv_find(device
);
521 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
528 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
531 if (!bdrv_is_removable(bs
)) {
532 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
533 bdrv_get_device_name(bs
));
536 if (bdrv_is_locked(bs
)) {
537 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
545 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
547 BlockDriverState
*bs
;
548 int force
= qdict_get_try_bool(qdict
, "force", 0);
549 const char *filename
= qdict_get_str(qdict
, "device");
551 bs
= bdrv_find(filename
);
553 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
556 return eject_device(mon
, bs
, force
);
559 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
562 BlockDriverState
*bs
;
565 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
567 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
571 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
572 if (err
== -EINVAL
) {
573 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
575 } else if (err
< 0) {
576 qerror_report(QERR_INVALID_PASSWORD
);
583 int do_change_block(Monitor
*mon
, const char *device
,
584 const char *filename
, const char *fmt
)
586 BlockDriverState
*bs
;
587 BlockDriver
*drv
= NULL
;
590 bs
= bdrv_find(device
);
592 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
596 drv
= bdrv_find_whitelisted_format(fmt
);
598 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
602 if (eject_device(mon
, bs
, 0) < 0) {
605 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
606 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
607 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
608 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
611 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);