]> git.proxmox.com Git - qemu.git/blob - blockdev.c
blockdev: New drive_get_by_blockdev()
[qemu.git] / blockdev.c
1 /*
2 * QEMU host block devices
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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 */
9
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"
17
18 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
19
20 QemuOpts *drive_add(const char *file, const char *fmt, ...)
21 {
22 va_list ap;
23 char optstr[1024];
24 QemuOpts *opts;
25
26 va_start(ap, fmt);
27 vsnprintf(optstr, sizeof(optstr), fmt, ap);
28 va_end(ap);
29
30 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
31 if (!opts) {
32 return NULL;
33 }
34 if (file)
35 qemu_opt_set(opts, "file", file);
36 return opts;
37 }
38
39 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
40 {
41 DriveInfo *dinfo;
42
43 /* seek interface, bus and unit */
44
45 QTAILQ_FOREACH(dinfo, &drives, next) {
46 if (dinfo->type == type &&
47 dinfo->bus == bus &&
48 dinfo->unit == unit)
49 return dinfo;
50 }
51
52 return NULL;
53 }
54
55 DriveInfo *drive_get_by_id(const char *id)
56 {
57 DriveInfo *dinfo;
58
59 QTAILQ_FOREACH(dinfo, &drives, next) {
60 if (strcmp(id, dinfo->id))
61 continue;
62 return dinfo;
63 }
64 return NULL;
65 }
66
67 int drive_get_max_bus(BlockInterfaceType type)
68 {
69 int max_bus;
70 DriveInfo *dinfo;
71
72 max_bus = -1;
73 QTAILQ_FOREACH(dinfo, &drives, next) {
74 if(dinfo->type == type &&
75 dinfo->bus > max_bus)
76 max_bus = dinfo->bus;
77 }
78 return max_bus;
79 }
80
81 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
82 {
83 DriveInfo *dinfo;
84
85 QTAILQ_FOREACH(dinfo, &drives, next) {
86 if (dinfo->bdrv == bs) {
87 return dinfo;
88 }
89 }
90 return NULL;
91 }
92
93 static void bdrv_format_print(void *opaque, const char *name)
94 {
95 fprintf(stderr, " %s", name);
96 }
97
98 void drive_uninit(DriveInfo *dinfo)
99 {
100 qemu_opts_del(dinfo->opts);
101 bdrv_delete(dinfo->bdrv);
102 QTAILQ_REMOVE(&drives, dinfo, next);
103 qemu_free(dinfo);
104 }
105
106 static int parse_block_error_action(const char *buf, int is_read)
107 {
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;
116 } else {
117 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
118 buf, is_read ? "read" : "write");
119 return -1;
120 }
121 }
122
123 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
124 {
125 const char *buf;
126 const char *file = NULL;
127 char devname[128];
128 const char *serial;
129 const char *mediastr = "";
130 BlockInterfaceType type;
131 enum { MEDIA_DISK, MEDIA_CDROM } media;
132 int bus_id, unit_id;
133 int cyls, heads, secs, translation;
134 BlockDriver *drv = NULL;
135 int max_devs;
136 int index;
137 int ro = 0;
138 int bdrv_flags = 0;
139 int on_read_error, on_write_error;
140 const char *devaddr;
141 DriveInfo *dinfo;
142 int snapshot = 0;
143 int ret;
144
145 *fatal_error = 1;
146
147 translation = BIOS_ATA_TRANSLATION_AUTO;
148
149 if (default_to_scsi) {
150 type = IF_SCSI;
151 max_devs = MAX_SCSI_DEVS;
152 pstrcpy(devname, sizeof(devname), "scsi");
153 } else {
154 type = IF_IDE;
155 max_devs = MAX_IDE_DEVS;
156 pstrcpy(devname, sizeof(devname), "ide");
157 }
158 media = MEDIA_DISK;
159
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);
164
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);
168
169 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
170 ro = qemu_opt_get_bool(opts, "readonly", 0);
171
172 file = qemu_opt_get(opts, "file");
173 serial = qemu_opt_get(opts, "serial");
174
175 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
176 pstrcpy(devname, sizeof(devname), buf);
177 if (!strcmp(buf, "ide")) {
178 type = IF_IDE;
179 max_devs = MAX_IDE_DEVS;
180 } else if (!strcmp(buf, "scsi")) {
181 type = IF_SCSI;
182 max_devs = MAX_SCSI_DEVS;
183 } else if (!strcmp(buf, "floppy")) {
184 type = IF_FLOPPY;
185 max_devs = 0;
186 } else if (!strcmp(buf, "pflash")) {
187 type = IF_PFLASH;
188 max_devs = 0;
189 } else if (!strcmp(buf, "mtd")) {
190 type = IF_MTD;
191 max_devs = 0;
192 } else if (!strcmp(buf, "sd")) {
193 type = IF_SD;
194 max_devs = 0;
195 } else if (!strcmp(buf, "virtio")) {
196 type = IF_VIRTIO;
197 max_devs = 0;
198 } else if (!strcmp(buf, "xen")) {
199 type = IF_XEN;
200 max_devs = 0;
201 } else if (!strcmp(buf, "none")) {
202 type = IF_NONE;
203 max_devs = 0;
204 } else {
205 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
206 return NULL;
207 }
208 }
209
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);
213 return NULL;
214 }
215 if (heads < 1 || (type == IF_IDE && heads > 16)) {
216 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
217 return NULL;
218 }
219 if (secs < 1 || (type == IF_IDE && secs > 63)) {
220 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
221 return NULL;
222 }
223 }
224
225 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
226 if (!cyls) {
227 fprintf(stderr,
228 "qemu: '%s' trans must be used with cyls,heads and secs\n",
229 buf);
230 return NULL;
231 }
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;
238 else {
239 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
240 return NULL;
241 }
242 }
243
244 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
245 if (!strcmp(buf, "disk")) {
246 media = MEDIA_DISK;
247 } else if (!strcmp(buf, "cdrom")) {
248 if (cyls || secs || heads) {
249 fprintf(stderr,
250 "qemu: '%s' invalid physical CHS format\n", buf);
251 return NULL;
252 }
253 media = MEDIA_CDROM;
254 } else {
255 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
256 return NULL;
257 }
258 }
259
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 */
270 } else {
271 fprintf(stderr, "qemu: invalid cache option\n");
272 return NULL;
273 }
274 }
275
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 */
282 } else {
283 fprintf(stderr, "qemu: invalid aio option\n");
284 return NULL;
285 }
286 }
287 #endif
288
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");
294 return NULL;
295 }
296 drv = bdrv_find_whitelisted_format(buf);
297 if (!drv) {
298 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
299 return NULL;
300 }
301 }
302
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");
307 return NULL;
308 }
309
310 on_write_error = parse_block_error_action(buf, 0);
311 if (on_write_error < 0) {
312 return NULL;
313 }
314 }
315
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");
320 return NULL;
321 }
322
323 on_read_error = parse_block_error_action(buf, 1);
324 if (on_read_error < 0) {
325 return NULL;
326 }
327 }
328
329 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
330 if (type != IF_VIRTIO) {
331 fprintf(stderr, "addr is not supported\n");
332 return NULL;
333 }
334 }
335
336 /* compute bus and unit according index */
337
338 if (index != -1) {
339 if (bus_id != 0 || unit_id != -1) {
340 fprintf(stderr,
341 "qemu: index cannot be used with bus and unit\n");
342 return NULL;
343 }
344 if (max_devs == 0)
345 {
346 unit_id = index;
347 bus_id = 0;
348 } else {
349 unit_id = index % max_devs;
350 bus_id = index / max_devs;
351 }
352 }
353
354 /* if user doesn't specify a unit_id,
355 * try to find the first free
356 */
357
358 if (unit_id == -1) {
359 unit_id = 0;
360 while (drive_get(type, bus_id, unit_id) != NULL) {
361 unit_id++;
362 if (max_devs && unit_id >= max_devs) {
363 unit_id -= max_devs;
364 bus_id++;
365 }
366 }
367 }
368
369 /* check unit id */
370
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);
374 return NULL;
375 }
376
377 /*
378 * ignore multiple definitions
379 */
380
381 if (drive_get(type, bus_id, unit_id) != NULL) {
382 *fatal_error = 0;
383 return NULL;
384 }
385
386 /* init */
387
388 dinfo = qemu_mallocz(sizeof(*dinfo));
389 if ((buf = qemu_opts_id(opts)) != NULL) {
390 dinfo->id = qemu_strdup(buf);
391 } else {
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";
396 if (max_devs)
397 snprintf(dinfo->id, 32, "%s%i%s%i",
398 devname, bus_id, mediastr, unit_id);
399 else
400 snprintf(dinfo->id, 32, "%s%s%i",
401 devname, mediastr, unit_id);
402 }
403 dinfo->bdrv = bdrv_new(dinfo->id);
404 dinfo->devaddr = devaddr;
405 dinfo->type = type;
406 dinfo->bus = bus_id;
407 dinfo->unit = unit_id;
408 dinfo->opts = opts;
409 if (serial)
410 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
411 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
412
413 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
414
415 switch(type) {
416 case IF_IDE:
417 case IF_SCSI:
418 case IF_XEN:
419 case IF_NONE:
420 switch(media) {
421 case MEDIA_DISK:
422 if (cyls != 0) {
423 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
424 bdrv_set_translation_hint(dinfo->bdrv, translation);
425 }
426 break;
427 case MEDIA_CDROM:
428 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
429 break;
430 }
431 break;
432 case IF_SD:
433 /* FIXME: This isn't really a floppy, but it's a reasonable
434 approximation. */
435 case IF_FLOPPY:
436 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
437 break;
438 case IF_PFLASH:
439 case IF_MTD:
440 break;
441 case IF_VIRTIO:
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);
446 if (devaddr)
447 qemu_opt_set(opts, "addr", devaddr);
448 break;
449 case IF_COUNT:
450 abort();
451 }
452 if (!file || !*file) {
453 *fatal_error = 0;
454 return NULL;
455 }
456 if (snapshot) {
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);
460 }
461
462 if (media == MEDIA_CDROM) {
463 /* CDROM is fine for any interface, don't check. */
464 ro = 1;
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");
468 return NULL;
469 }
470 }
471
472 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
473
474 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
475 if (ret < 0) {
476 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
477 file, strerror(-ret));
478 return NULL;
479 }
480
481 if (bdrv_key_required(dinfo->bdrv))
482 autostart = 0;
483 *fatal_error = 0;
484 return dinfo;
485 }
486
487 void do_commit(Monitor *mon, const QDict *qdict)
488 {
489 const char *device = qdict_get_str(qdict, "device");
490 BlockDriverState *bs;
491
492 if (!strcmp(device, "all")) {
493 bdrv_commit_all();
494 } else {
495 bs = bdrv_find(device);
496 if (!bs) {
497 qerror_report(QERR_DEVICE_NOT_FOUND, device);
498 return;
499 }
500 bdrv_commit(bs);
501 }
502 }
503
504 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
505 {
506 if (!force) {
507 if (!bdrv_is_removable(bs)) {
508 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
509 bdrv_get_device_name(bs));
510 return -1;
511 }
512 if (bdrv_is_locked(bs)) {
513 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
514 return -1;
515 }
516 }
517 bdrv_close(bs);
518 return 0;
519 }
520
521 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
522 {
523 BlockDriverState *bs;
524 int force = qdict_get_int(qdict, "force");
525 const char *filename = qdict_get_str(qdict, "device");
526
527 bs = bdrv_find(filename);
528 if (!bs) {
529 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
530 return -1;
531 }
532 return eject_device(mon, bs, force);
533 }
534
535 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
536 QObject **ret_data)
537 {
538 BlockDriverState *bs;
539 int err;
540
541 bs = bdrv_find(qdict_get_str(qdict, "device"));
542 if (!bs) {
543 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
544 return -1;
545 }
546
547 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
548 if (err == -EINVAL) {
549 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
550 return -1;
551 } else if (err < 0) {
552 qerror_report(QERR_INVALID_PASSWORD);
553 return -1;
554 }
555
556 return 0;
557 }
558
559 int do_change_block(Monitor *mon, const char *device,
560 const char *filename, const char *fmt)
561 {
562 BlockDriverState *bs;
563 BlockDriver *drv = NULL;
564 int bdrv_flags;
565
566 bs = bdrv_find(device);
567 if (!bs) {
568 qerror_report(QERR_DEVICE_NOT_FOUND, device);
569 return -1;
570 }
571 if (fmt) {
572 drv = bdrv_find_whitelisted_format(fmt);
573 if (!drv) {
574 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
575 return -1;
576 }
577 }
578 if (eject_device(mon, bs, 0) < 0) {
579 return -1;
580 }
581 bdrv_flags = bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM ? 0 : BDRV_O_RDWR;
582 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
583 qerror_report(QERR_OPEN_FILE_FAILED, filename);
584 return -1;
585 }
586 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
587 }