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 static void bdrv_format_print(void *opaque
, const char *name
)
83 fprintf(stderr
, " %s", name
);
86 void drive_uninit(DriveInfo
*dinfo
)
88 qemu_opts_del(dinfo
->opts
);
89 bdrv_delete(dinfo
->bdrv
);
90 QTAILQ_REMOVE(&drives
, dinfo
, next
);
94 static int parse_block_error_action(const char *buf
, int is_read
)
96 if (!strcmp(buf
, "ignore")) {
97 return BLOCK_ERR_IGNORE
;
98 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
99 return BLOCK_ERR_STOP_ENOSPC
;
100 } else if (!strcmp(buf
, "stop")) {
101 return BLOCK_ERR_STOP_ANY
;
102 } else if (!strcmp(buf
, "report")) {
103 return BLOCK_ERR_REPORT
;
105 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
106 buf
, is_read
? "read" : "write");
111 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
114 const char *file
= NULL
;
117 const char *mediastr
= "";
118 BlockInterfaceType type
;
119 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
121 int cyls
, heads
, secs
, translation
;
122 BlockDriver
*drv
= NULL
;
127 int on_read_error
, on_write_error
;
135 translation
= BIOS_ATA_TRANSLATION_AUTO
;
137 if (default_to_scsi
) {
139 max_devs
= MAX_SCSI_DEVS
;
140 pstrcpy(devname
, sizeof(devname
), "scsi");
143 max_devs
= MAX_IDE_DEVS
;
144 pstrcpy(devname
, sizeof(devname
), "ide");
148 /* extract parameters */
149 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
150 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
151 index
= qemu_opt_get_number(opts
, "index", -1);
153 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
154 heads
= qemu_opt_get_number(opts
, "heads", 0);
155 secs
= qemu_opt_get_number(opts
, "secs", 0);
157 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
158 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
160 file
= qemu_opt_get(opts
, "file");
161 serial
= qemu_opt_get(opts
, "serial");
163 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
164 pstrcpy(devname
, sizeof(devname
), buf
);
165 if (!strcmp(buf
, "ide")) {
167 max_devs
= MAX_IDE_DEVS
;
168 } else if (!strcmp(buf
, "scsi")) {
170 max_devs
= MAX_SCSI_DEVS
;
171 } else if (!strcmp(buf
, "floppy")) {
174 } else if (!strcmp(buf
, "pflash")) {
177 } else if (!strcmp(buf
, "mtd")) {
180 } else if (!strcmp(buf
, "sd")) {
183 } else if (!strcmp(buf
, "virtio")) {
186 } else if (!strcmp(buf
, "xen")) {
189 } else if (!strcmp(buf
, "none")) {
193 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
198 if (cyls
|| heads
|| secs
) {
199 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
200 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
203 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
204 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
207 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
208 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
213 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
216 "qemu: '%s' trans must be used with cyls,heads and secs\n",
220 if (!strcmp(buf
, "none"))
221 translation
= BIOS_ATA_TRANSLATION_NONE
;
222 else if (!strcmp(buf
, "lba"))
223 translation
= BIOS_ATA_TRANSLATION_LBA
;
224 else if (!strcmp(buf
, "auto"))
225 translation
= BIOS_ATA_TRANSLATION_AUTO
;
227 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
232 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
233 if (!strcmp(buf
, "disk")) {
235 } else if (!strcmp(buf
, "cdrom")) {
236 if (cyls
|| secs
|| heads
) {
238 "qemu: '%s' invalid physical CHS format\n", buf
);
243 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
248 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
249 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
250 bdrv_flags
|= BDRV_O_NOCACHE
;
251 } else if (!strcmp(buf
, "writeback")) {
252 bdrv_flags
|= BDRV_O_CACHE_WB
;
253 } else if (!strcmp(buf
, "unsafe")) {
254 bdrv_flags
|= BDRV_O_CACHE_WB
;
255 bdrv_flags
|= BDRV_O_NO_FLUSH
;
256 } else if (!strcmp(buf
, "writethrough")) {
257 /* this is the default */
259 fprintf(stderr
, "qemu: invalid cache option\n");
264 #ifdef CONFIG_LINUX_AIO
265 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
266 if (!strcmp(buf
, "native")) {
267 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
268 } else if (!strcmp(buf
, "threads")) {
269 /* this is the default */
271 fprintf(stderr
, "qemu: invalid aio option\n");
277 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
278 if (strcmp(buf
, "?") == 0) {
279 fprintf(stderr
, "qemu: Supported formats:");
280 bdrv_iterate_format(bdrv_format_print
, NULL
);
281 fprintf(stderr
, "\n");
284 drv
= bdrv_find_whitelisted_format(buf
);
286 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
291 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
292 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
293 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
294 fprintf(stderr
, "werror is no supported by this format\n");
298 on_write_error
= parse_block_error_action(buf
, 0);
299 if (on_write_error
< 0) {
304 on_read_error
= BLOCK_ERR_REPORT
;
305 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
306 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
307 fprintf(stderr
, "rerror is no supported by this format\n");
311 on_read_error
= parse_block_error_action(buf
, 1);
312 if (on_read_error
< 0) {
317 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
318 if (type
!= IF_VIRTIO
) {
319 fprintf(stderr
, "addr is not supported\n");
324 /* compute bus and unit according index */
327 if (bus_id
!= 0 || unit_id
!= -1) {
329 "qemu: index cannot be used with bus and unit\n");
337 unit_id
= index
% max_devs
;
338 bus_id
= index
/ max_devs
;
342 /* if user doesn't specify a unit_id,
343 * try to find the first free
348 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
350 if (max_devs
&& unit_id
>= max_devs
) {
359 if (max_devs
&& unit_id
>= max_devs
) {
360 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
361 unit_id
, max_devs
- 1);
366 * ignore multiple definitions
369 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
376 dinfo
= qemu_mallocz(sizeof(*dinfo
));
377 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
378 dinfo
->id
= qemu_strdup(buf
);
380 /* no id supplied -> create one */
381 dinfo
->id
= qemu_mallocz(32);
382 if (type
== IF_IDE
|| type
== IF_SCSI
)
383 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
385 snprintf(dinfo
->id
, 32, "%s%i%s%i",
386 devname
, bus_id
, mediastr
, unit_id
);
388 snprintf(dinfo
->id
, 32, "%s%s%i",
389 devname
, mediastr
, unit_id
);
391 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
392 dinfo
->devaddr
= devaddr
;
395 dinfo
->unit
= unit_id
;
398 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
399 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
401 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
411 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
412 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
416 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
421 /* FIXME: This isn't really a floppy, but it's a reasonable
424 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
430 /* add virtio block device */
431 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
432 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
433 qemu_opt_set(opts
, "drive", dinfo
->id
);
435 qemu_opt_set(opts
, "addr", devaddr
);
440 if (!file
|| !*file
) {
445 /* always use cache=unsafe with snapshot */
446 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
447 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
450 if (media
== MEDIA_CDROM
) {
451 /* CDROM is fine for any interface, don't check. */
453 } else if (ro
== 1) {
454 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
455 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
460 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
462 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
464 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
465 file
, strerror(-ret
));
469 if (bdrv_key_required(dinfo
->bdrv
))
475 void do_commit(Monitor
*mon
, const QDict
*qdict
)
477 const char *device
= qdict_get_str(qdict
, "device");
478 BlockDriverState
*bs
;
480 if (!strcmp(device
, "all")) {
483 bs
= bdrv_find(device
);
485 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
492 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
495 if (!bdrv_is_removable(bs
)) {
496 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
497 bdrv_get_device_name(bs
));
500 if (bdrv_is_locked(bs
)) {
501 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
509 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
511 BlockDriverState
*bs
;
512 int force
= qdict_get_int(qdict
, "force");
513 const char *filename
= qdict_get_str(qdict
, "device");
515 bs
= bdrv_find(filename
);
517 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
520 return eject_device(mon
, bs
, force
);
523 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
526 BlockDriverState
*bs
;
529 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
531 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
535 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
536 if (err
== -EINVAL
) {
537 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
539 } else if (err
< 0) {
540 qerror_report(QERR_INVALID_PASSWORD
);
547 int do_change_block(Monitor
*mon
, const char *device
,
548 const char *filename
, const char *fmt
)
550 BlockDriverState
*bs
;
551 BlockDriver
*drv
= NULL
;
554 bs
= bdrv_find(device
);
556 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
560 drv
= bdrv_find_whitelisted_format(fmt
);
562 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
566 if (eject_device(mon
, bs
, 0) < 0) {
569 bdrv_flags
= bdrv_get_type_hint(bs
) == BDRV_TYPE_CDROM
? 0 : BDRV_O_RDWR
;
570 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
571 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
574 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);