kvm test: setup_vm should map APIC
[qemu-kvm/stefanha.git] / blockdev.c
blob7e2dba4731637343331810836bcf387a663551d8
1 /*
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.
8 */
10 #include "block.h"
11 #include "blockdev.h"
12 #include "monitor.h"
13 #include "qerror.h"
14 #include "qemu-option.h"
15 #include "qemu-config.h"
16 #include "sysemu.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, ...)
24 va_list ap;
25 char optstr[1024];
26 QemuOpts *opts;
28 va_start(ap, fmt);
29 vsnprintf(optstr, sizeof(optstr), fmt, ap);
30 va_end(ap);
32 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
33 if (!opts) {
34 return NULL;
36 if (file)
37 qemu_opt_set(opts, "file", file);
38 return opts;
41 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
43 DriveInfo *dinfo;
45 /* seek interface, bus and unit */
47 QTAILQ_FOREACH(dinfo, &drives, next) {
48 if (dinfo->type == type &&
49 dinfo->bus == bus &&
50 dinfo->unit == unit)
51 return dinfo;
54 return NULL;
57 DriveInfo *drive_get_by_id(const char *id)
59 DriveInfo *dinfo;
61 QTAILQ_FOREACH(dinfo, &drives, next) {
62 if (strcmp(id, dinfo->id))
63 continue;
64 return dinfo;
66 return NULL;
69 int drive_get_max_bus(BlockInterfaceType type)
71 int max_bus;
72 DriveInfo *dinfo;
74 max_bus = -1;
75 QTAILQ_FOREACH(dinfo, &drives, next) {
76 if(dinfo->type == type &&
77 dinfo->bus > max_bus)
78 max_bus = dinfo->bus;
80 return max_bus;
83 const char *drive_get_serial(BlockDriverState *bdrv)
85 DriveInfo *dinfo;
87 QTAILQ_FOREACH(dinfo, &drives, next) {
88 if (dinfo->bdrv == bdrv)
89 return dinfo->serial;
92 return "\0";
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);
105 qemu_free(dinfo);
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;
118 } else {
119 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
120 buf, is_read ? "read" : "write");
121 return -1;
125 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
127 const char *buf;
128 const char *file = NULL;
129 char devname[128];
130 const char *serial;
131 const char *mediastr = "";
132 BlockInterfaceType type;
133 enum { MEDIA_DISK, MEDIA_CDROM } media;
134 int bus_id, unit_id;
135 int cyls, heads, secs, translation;
136 BlockDriver *drv = NULL;
137 int max_devs;
138 int index;
139 int ro = 0;
140 int bdrv_flags = 0;
141 int on_read_error, on_write_error;
142 const char *devaddr;
143 DriveInfo *dinfo;
144 int snapshot = 0;
145 int ret;
147 *fatal_error = 1;
149 translation = BIOS_ATA_TRANSLATION_AUTO;
151 if (default_to_scsi) {
152 type = IF_SCSI;
153 max_devs = MAX_SCSI_DEVS;
154 pstrcpy(devname, sizeof(devname), "scsi");
155 } else {
156 type = IF_IDE;
157 max_devs = MAX_IDE_DEVS;
158 pstrcpy(devname, sizeof(devname), "ide");
160 media = MEDIA_DISK;
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")) {
180 type = IF_IDE;
181 max_devs = MAX_IDE_DEVS;
182 } else if (!strcmp(buf, "scsi")) {
183 type = IF_SCSI;
184 max_devs = MAX_SCSI_DEVS;
185 } else if (!strcmp(buf, "floppy")) {
186 type = IF_FLOPPY;
187 max_devs = 0;
188 } else if (!strcmp(buf, "pflash")) {
189 type = IF_PFLASH;
190 max_devs = 0;
191 } else if (!strcmp(buf, "mtd")) {
192 type = IF_MTD;
193 max_devs = 0;
194 } else if (!strcmp(buf, "sd")) {
195 type = IF_SD;
196 max_devs = 0;
197 } else if (!strcmp(buf, "virtio")) {
198 type = IF_VIRTIO;
199 max_devs = 0;
200 } else if (!strcmp(buf, "xen")) {
201 type = IF_XEN;
202 max_devs = 0;
203 } else if (!strcmp(buf, "none")) {
204 type = IF_NONE;
205 max_devs = 0;
206 } else {
207 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
208 return NULL;
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);
215 return NULL;
217 if (heads < 1 || (type == IF_IDE && heads > 16)) {
218 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
219 return NULL;
221 if (secs < 1 || (type == IF_IDE && secs > 63)) {
222 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
223 return NULL;
227 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
228 if (!cyls) {
229 fprintf(stderr,
230 "qemu: '%s' trans must be used with cyls,heads and secs\n",
231 buf);
232 return NULL;
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;
240 else {
241 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
242 return NULL;
246 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
247 if (!strcmp(buf, "disk")) {
248 media = MEDIA_DISK;
249 } else if (!strcmp(buf, "cdrom")) {
250 if (cyls || secs || heads) {
251 fprintf(stderr,
252 "qemu: '%s' invalid physical CHS format\n", buf);
253 return NULL;
255 media = MEDIA_CDROM;
256 } else {
257 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
258 return NULL;
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 */
272 } else {
273 fprintf(stderr, "qemu: invalid cache option\n");
274 return NULL;
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 */
284 } else {
285 fprintf(stderr, "qemu: invalid aio option\n");
286 return NULL;
289 #endif
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");
296 return NULL;
298 drv = bdrv_find_whitelisted_format(buf);
299 if (!drv) {
300 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
301 return NULL;
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");
309 return NULL;
312 on_write_error = parse_block_error_action(buf, 0);
313 if (on_write_error < 0) {
314 return NULL;
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");
322 return NULL;
325 on_read_error = parse_block_error_action(buf, 1);
326 if (on_read_error < 0) {
327 return NULL;
331 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
332 if (type != IF_VIRTIO) {
333 fprintf(stderr, "addr is not supported\n");
334 return NULL;
338 /* compute bus and unit according index */
340 if (index != -1) {
341 if (bus_id != 0 || unit_id != -1) {
342 fprintf(stderr,
343 "qemu: index cannot be used with bus and unit\n");
344 return NULL;
346 if (max_devs == 0)
348 unit_id = index;
349 bus_id = 0;
350 } else {
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
360 if (unit_id == -1) {
361 unit_id = 0;
362 while (drive_get(type, bus_id, unit_id) != NULL) {
363 unit_id++;
364 if (max_devs && unit_id >= max_devs) {
365 unit_id -= max_devs;
366 bus_id++;
371 /* check unit id */
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);
376 return NULL;
380 * ignore multiple definitions
383 if (drive_get(type, bus_id, unit_id) != NULL) {
384 *fatal_error = 0;
385 return NULL;
388 /* init */
390 dinfo = qemu_mallocz(sizeof(*dinfo));
391 if ((buf = qemu_opts_id(opts)) != NULL) {
392 dinfo->id = qemu_strdup(buf);
393 } else {
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";
398 if (max_devs)
399 snprintf(dinfo->id, 32, "%s%i%s%i",
400 devname, bus_id, mediastr, unit_id);
401 else
402 snprintf(dinfo->id, 32, "%s%s%i",
403 devname, mediastr, unit_id);
405 dinfo->bdrv = bdrv_new(dinfo->id);
406 dinfo->devaddr = devaddr;
407 dinfo->type = type;
408 dinfo->bus = bus_id;
409 dinfo->unit = unit_id;
410 dinfo->opts = opts;
411 if (serial)
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);
417 switch(type) {
418 case IF_IDE:
419 case IF_SCSI:
420 case IF_XEN:
421 case IF_NONE:
422 switch(media) {
423 case MEDIA_DISK:
424 if (cyls != 0) {
425 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
426 bdrv_set_translation_hint(dinfo->bdrv, translation);
428 break;
429 case MEDIA_CDROM:
430 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
431 break;
433 break;
434 case IF_SD:
435 /* FIXME: This isn't really a floppy, but it's a reasonable
436 approximation. */
437 case IF_FLOPPY:
438 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
439 break;
440 case IF_PFLASH:
441 case IF_MTD:
442 break;
443 case IF_VIRTIO:
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);
448 if (devaddr)
449 qemu_opt_set(opts, "addr", devaddr);
450 break;
451 case IF_COUNT:
452 abort();
454 if (!file || !*file) {
455 *fatal_error = 0;
456 return NULL;
458 if (snapshot) {
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. */
466 ro = 1;
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");
470 return NULL;
474 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
476 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
477 if (ret < 0) {
478 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
479 file, strerror(-ret));
480 return NULL;
483 if (bdrv_key_required(dinfo->bdrv))
484 autostart = 0;
485 *fatal_error = 0;
486 return dinfo;
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")) {
495 bdrv_commit_all();
496 } else {
497 bs = bdrv_find(device);
498 if (!bs) {
499 qerror_report(QERR_DEVICE_NOT_FOUND, device);
500 return;
502 bdrv_commit(bs);
506 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
508 if (bdrv_is_inserted(bs)) {
509 if (!force) {
510 if (!bdrv_is_removable(bs)) {
511 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
512 bdrv_get_device_name(bs));
513 return -1;
515 if (bdrv_is_locked(bs)) {
516 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
517 return -1;
520 bdrv_close(bs);
522 return 0;
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);
532 if (!bs) {
533 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
534 return -1;
536 return eject_device(mon, bs, force);
539 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
540 QObject **ret_data)
542 BlockDriverState *bs;
543 int err;
545 bs = bdrv_find(qdict_get_str(qdict, "device"));
546 if (!bs) {
547 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
548 return -1;
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));
554 return -1;
555 } else if (err < 0) {
556 qerror_report(QERR_INVALID_PASSWORD);
557 return -1;
560 return 0;
563 int do_change_block(Monitor *mon, const char *device,
564 const char *filename, const char *fmt)
566 BlockDriverState *bs;
567 BlockDriver *drv = NULL;
568 int bdrv_flags;
570 bs = bdrv_find(device);
571 if (!bs) {
572 qerror_report(QERR_DEVICE_NOT_FOUND, device);
573 return -1;
575 if (fmt) {
576 drv = bdrv_find_whitelisted_format(fmt);
577 if (!drv) {
578 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
579 return -1;
582 if (eject_device(mon, bs, 0) < 0) {
583 return -1;
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);
588 return -1;
590 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);