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
);
20 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
27 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
30 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
35 qemu_opt_set(opts
, "file", file
);
39 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
43 /* seek interface, bus and unit */
45 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
46 if (dinfo
->type
== type
&&
55 DriveInfo
*drive_get_by_id(const char *id
)
59 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
60 if (strcmp(id
, dinfo
->id
))
67 int drive_get_max_bus(BlockInterfaceType type
)
73 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
74 if(dinfo
->type
== type
&&
81 const char *drive_get_serial(BlockDriverState
*bdrv
)
85 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
86 if (dinfo
->bdrv
== bdrv
)
93 static void bdrv_format_print(void *opaque
, const char *name
)
95 fprintf(stderr
, " %s", name
);
98 void drive_uninit(DriveInfo
*dinfo
)
100 qemu_opts_del(dinfo
->opts
);
101 bdrv_delete(dinfo
->bdrv
);
102 QTAILQ_REMOVE(&drives
, dinfo
, next
);
106 static int parse_block_error_action(const char *buf
, int is_read
)
108 if (!strcmp(buf
, "ignore")) {
109 return BLOCK_ERR_IGNORE
;
110 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
111 return BLOCK_ERR_STOP_ENOSPC
;
112 } else if (!strcmp(buf
, "stop")) {
113 return BLOCK_ERR_STOP_ANY
;
114 } else if (!strcmp(buf
, "report")) {
115 return BLOCK_ERR_REPORT
;
117 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
118 buf
, is_read
? "read" : "write");
123 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
126 const char *file
= NULL
;
129 const char *mediastr
= "";
130 BlockInterfaceType type
;
131 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
133 int cyls
, heads
, secs
, translation
;
134 BlockDriver
*drv
= NULL
;
139 int on_read_error
, on_write_error
;
147 translation
= BIOS_ATA_TRANSLATION_AUTO
;
149 if (default_to_scsi
) {
151 max_devs
= MAX_SCSI_DEVS
;
152 pstrcpy(devname
, sizeof(devname
), "scsi");
155 max_devs
= MAX_IDE_DEVS
;
156 pstrcpy(devname
, sizeof(devname
), "ide");
160 /* extract parameters */
161 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
162 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
163 index
= qemu_opt_get_number(opts
, "index", -1);
165 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
166 heads
= qemu_opt_get_number(opts
, "heads", 0);
167 secs
= qemu_opt_get_number(opts
, "secs", 0);
169 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
170 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
172 file
= qemu_opt_get(opts
, "file");
173 serial
= qemu_opt_get(opts
, "serial");
175 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
176 pstrcpy(devname
, sizeof(devname
), buf
);
177 if (!strcmp(buf
, "ide")) {
179 max_devs
= MAX_IDE_DEVS
;
180 } else if (!strcmp(buf
, "scsi")) {
182 max_devs
= MAX_SCSI_DEVS
;
183 } else if (!strcmp(buf
, "floppy")) {
186 } else if (!strcmp(buf
, "pflash")) {
189 } else if (!strcmp(buf
, "mtd")) {
192 } else if (!strcmp(buf
, "sd")) {
195 } else if (!strcmp(buf
, "virtio")) {
198 } else if (!strcmp(buf
, "xen")) {
201 } else if (!strcmp(buf
, "none")) {
205 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
210 if (cyls
|| heads
|| secs
) {
211 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
212 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
215 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
216 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
219 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
220 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
225 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
228 "qemu: '%s' trans must be used with cyls,heads and secs\n",
232 if (!strcmp(buf
, "none"))
233 translation
= BIOS_ATA_TRANSLATION_NONE
;
234 else if (!strcmp(buf
, "lba"))
235 translation
= BIOS_ATA_TRANSLATION_LBA
;
236 else if (!strcmp(buf
, "auto"))
237 translation
= BIOS_ATA_TRANSLATION_AUTO
;
239 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
244 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
245 if (!strcmp(buf
, "disk")) {
247 } else if (!strcmp(buf
, "cdrom")) {
248 if (cyls
|| secs
|| heads
) {
250 "qemu: '%s' invalid physical CHS format\n", buf
);
255 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
260 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
261 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
262 bdrv_flags
|= BDRV_O_NOCACHE
;
263 } else if (!strcmp(buf
, "writeback")) {
264 bdrv_flags
|= BDRV_O_CACHE_WB
;
265 } else if (!strcmp(buf
, "unsafe")) {
266 bdrv_flags
|= BDRV_O_CACHE_WB
;
267 bdrv_flags
|= BDRV_O_NO_FLUSH
;
268 } else if (!strcmp(buf
, "writethrough")) {
269 /* this is the default */
271 fprintf(stderr
, "qemu: invalid cache option\n");
276 #ifdef CONFIG_LINUX_AIO
277 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
278 if (!strcmp(buf
, "native")) {
279 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
280 } else if (!strcmp(buf
, "threads")) {
281 /* this is the default */
283 fprintf(stderr
, "qemu: invalid aio option\n");
289 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
290 if (strcmp(buf
, "?") == 0) {
291 fprintf(stderr
, "qemu: Supported formats:");
292 bdrv_iterate_format(bdrv_format_print
, NULL
);
293 fprintf(stderr
, "\n");
296 drv
= bdrv_find_whitelisted_format(buf
);
298 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
303 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
304 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
305 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
306 fprintf(stderr
, "werror is no supported by this format\n");
310 on_write_error
= parse_block_error_action(buf
, 0);
311 if (on_write_error
< 0) {
316 on_read_error
= BLOCK_ERR_REPORT
;
317 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
318 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
319 fprintf(stderr
, "rerror is no supported by this format\n");
323 on_read_error
= parse_block_error_action(buf
, 1);
324 if (on_read_error
< 0) {
329 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
330 if (type
!= IF_VIRTIO
) {
331 fprintf(stderr
, "addr is not supported\n");
336 /* compute bus and unit according index */
339 if (bus_id
!= 0 || unit_id
!= -1) {
341 "qemu: index cannot be used with bus and unit\n");
349 unit_id
= index
% max_devs
;
350 bus_id
= index
/ max_devs
;
354 /* if user doesn't specify a unit_id,
355 * try to find the first free
360 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
362 if (max_devs
&& unit_id
>= max_devs
) {
371 if (max_devs
&& unit_id
>= max_devs
) {
372 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
373 unit_id
, max_devs
- 1);
378 * ignore multiple definitions
381 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
388 dinfo
= qemu_mallocz(sizeof(*dinfo
));
389 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
390 dinfo
->id
= qemu_strdup(buf
);
392 /* no id supplied -> create one */
393 dinfo
->id
= qemu_mallocz(32);
394 if (type
== IF_IDE
|| type
== IF_SCSI
)
395 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
397 snprintf(dinfo
->id
, 32, "%s%i%s%i",
398 devname
, bus_id
, mediastr
, unit_id
);
400 snprintf(dinfo
->id
, 32, "%s%s%i",
401 devname
, mediastr
, unit_id
);
403 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
404 dinfo
->devaddr
= devaddr
;
407 dinfo
->unit
= unit_id
;
410 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
411 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
413 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
423 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
424 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
428 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
433 /* FIXME: This isn't really a floppy, but it's a reasonable
436 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
442 /* add virtio block device */
443 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
444 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
445 qemu_opt_set(opts
, "drive", dinfo
->id
);
447 qemu_opt_set(opts
, "addr", devaddr
);
452 if (!file
|| !*file
) {
457 /* always use cache=unsafe with snapshot */
458 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
459 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
462 if (media
== MEDIA_CDROM
) {
463 /* CDROM is fine for any interface, don't check. */
465 } else if (ro
== 1) {
466 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
467 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
472 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
474 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
476 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
477 file
, strerror(-ret
));
481 if (bdrv_key_required(dinfo
->bdrv
))
487 void do_commit(Monitor
*mon
, const QDict
*qdict
)
489 const char *device
= qdict_get_str(qdict
, "device");
490 BlockDriverState
*bs
;
492 if (!strcmp(device
, "all")) {
495 bs
= bdrv_find(device
);
497 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
504 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
506 if (bdrv_is_inserted(bs
)) {
508 if (!bdrv_is_removable(bs
)) {
509 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
510 bdrv_get_device_name(bs
));
513 if (bdrv_is_locked(bs
)) {
514 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
523 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
525 BlockDriverState
*bs
;
526 int force
= qdict_get_int(qdict
, "force");
527 const char *filename
= qdict_get_str(qdict
, "device");
529 bs
= bdrv_find(filename
);
531 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
534 return eject_device(mon
, bs
, force
);
537 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
540 BlockDriverState
*bs
;
543 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
545 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
549 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
550 if (err
== -EINVAL
) {
551 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
553 } else if (err
< 0) {
554 qerror_report(QERR_INVALID_PASSWORD
);
561 int do_change_block(Monitor
*mon
, const char *device
,
562 const char *filename
, const char *fmt
)
564 BlockDriverState
*bs
;
565 BlockDriver
*drv
= NULL
;
568 bs
= bdrv_find(device
);
570 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
574 drv
= bdrv_find_whitelisted_format(fmt
);
576 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
580 if (eject_device(mon
, bs
, 0) < 0) {
583 bdrv_flags
= bdrv_get_type_hint(bs
) == BDRV_TYPE_CDROM
? 0 : BDRV_O_RDWR
;
584 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
585 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
588 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);