Revision 666daa68 vl.c

b/vl.c
140 140
#include "qemu-char.h"
141 141
#include "cache-utils.h"
142 142
#include "block.h"
143
#include "block_int.h"
143
#include "blockdev.h"
144 144
#include "block-migration.h"
145 145
#include "dma.h"
146 146
#include "audio/audio.h"
......
172 172

  
173 173
static const char *data_dir;
174 174
const char *bios_name = NULL;
175
struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
176 175
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
177 176
DisplayType display_type = DT_DEFAULT;
178 177
const char* keyboard_layout = NULL;
......
651 650
#define MTD_ALIAS "if=mtd"
652 651
#define SD_ALIAS "index=0,if=sd"
653 652

  
654
QemuOpts *drive_add(const char *file, const char *fmt, ...)
655
{
656
    va_list ap;
657
    char optstr[1024];
658
    QemuOpts *opts;
659

  
660
    va_start(ap, fmt);
661
    vsnprintf(optstr, sizeof(optstr), fmt, ap);
662
    va_end(ap);
663

  
664
    opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
665
    if (!opts) {
666
        return NULL;
667
    }
668
    if (file)
669
        qemu_opt_set(opts, "file", file);
670
    return opts;
671
}
672

  
673
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
674
{
675
    DriveInfo *dinfo;
676

  
677
    /* seek interface, bus and unit */
678

  
679
    QTAILQ_FOREACH(dinfo, &drives, next) {
680
        if (dinfo->type == type &&
681
	    dinfo->bus == bus &&
682
	    dinfo->unit == unit)
683
            return dinfo;
684
    }
685

  
686
    return NULL;
687
}
688

  
689
DriveInfo *drive_get_by_id(const char *id)
690
{
691
    DriveInfo *dinfo;
692

  
693
    QTAILQ_FOREACH(dinfo, &drives, next) {
694
        if (strcmp(id, dinfo->id))
695
            continue;
696
        return dinfo;
697
    }
698
    return NULL;
699
}
700

  
701
int drive_get_max_bus(BlockInterfaceType type)
702
{
703
    int max_bus;
704
    DriveInfo *dinfo;
705

  
706
    max_bus = -1;
707
    QTAILQ_FOREACH(dinfo, &drives, next) {
708
        if(dinfo->type == type &&
709
           dinfo->bus > max_bus)
710
            max_bus = dinfo->bus;
711
    }
712
    return max_bus;
713
}
714

  
715
const char *drive_get_serial(BlockDriverState *bdrv)
716
{
717
    DriveInfo *dinfo;
718

  
719
    QTAILQ_FOREACH(dinfo, &drives, next) {
720
        if (dinfo->bdrv == bdrv)
721
            return dinfo->serial;
722
    }
723

  
724
    return "\0";
725
}
726

  
727
BlockInterfaceErrorAction drive_get_on_error(
728
    BlockDriverState *bdrv, int is_read)
729
{
730
    DriveInfo *dinfo;
731

  
732
    QTAILQ_FOREACH(dinfo, &drives, next) {
733
        if (dinfo->bdrv == bdrv)
734
            return is_read ? dinfo->on_read_error : dinfo->on_write_error;
735
    }
736

  
737
    return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
738
}
739

  
740
static void bdrv_format_print(void *opaque, const char *name)
741
{
742
    fprintf(stderr, " %s", name);
743
}
744

  
745
void drive_uninit(DriveInfo *dinfo)
746
{
747
    qemu_opts_del(dinfo->opts);
748
    bdrv_delete(dinfo->bdrv);
749
    QTAILQ_REMOVE(&drives, dinfo, next);
750
    qemu_free(dinfo);
751
}
752

  
753
static int parse_block_error_action(const char *buf, int is_read)
754
{
755
    if (!strcmp(buf, "ignore")) {
756
        return BLOCK_ERR_IGNORE;
757
    } else if (!is_read && !strcmp(buf, "enospc")) {
758
        return BLOCK_ERR_STOP_ENOSPC;
759
    } else if (!strcmp(buf, "stop")) {
760
        return BLOCK_ERR_STOP_ANY;
761
    } else if (!strcmp(buf, "report")) {
762
        return BLOCK_ERR_REPORT;
763
    } else {
764
        fprintf(stderr, "qemu: '%s' invalid %s error action\n",
765
            buf, is_read ? "read" : "write");
766
        return -1;
767
    }
768
}
769

  
770
DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
771
{
772
    const char *buf;
773
    const char *file = NULL;
774
    char devname[128];
775
    const char *serial;
776
    const char *mediastr = "";
777
    BlockInterfaceType type;
778
    enum { MEDIA_DISK, MEDIA_CDROM } media;
779
    int bus_id, unit_id;
780
    int cyls, heads, secs, translation;
781
    BlockDriver *drv = NULL;
782
    int max_devs;
783
    int index;
784
    int ro = 0;
785
    int bdrv_flags = 0;
786
    int on_read_error, on_write_error;
787
    const char *devaddr;
788
    DriveInfo *dinfo;
789
    int snapshot = 0;
790
    int ret;
791

  
792
    *fatal_error = 1;
793

  
794
    translation = BIOS_ATA_TRANSLATION_AUTO;
795

  
796
    if (default_to_scsi) {
797
        type = IF_SCSI;
798
        max_devs = MAX_SCSI_DEVS;
799
        pstrcpy(devname, sizeof(devname), "scsi");
800
    } else {
801
        type = IF_IDE;
802
        max_devs = MAX_IDE_DEVS;
803
        pstrcpy(devname, sizeof(devname), "ide");
804
    }
805
    media = MEDIA_DISK;
806

  
807
    /* extract parameters */
808
    bus_id  = qemu_opt_get_number(opts, "bus", 0);
809
    unit_id = qemu_opt_get_number(opts, "unit", -1);
810
    index   = qemu_opt_get_number(opts, "index", -1);
811

  
812
    cyls  = qemu_opt_get_number(opts, "cyls", 0);
813
    heads = qemu_opt_get_number(opts, "heads", 0);
814
    secs  = qemu_opt_get_number(opts, "secs", 0);
815

  
816
    snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
817
    ro = qemu_opt_get_bool(opts, "readonly", 0);
818

  
819
    file = qemu_opt_get(opts, "file");
820
    serial = qemu_opt_get(opts, "serial");
821

  
822
    if ((buf = qemu_opt_get(opts, "if")) != NULL) {
823
        pstrcpy(devname, sizeof(devname), buf);
824
        if (!strcmp(buf, "ide")) {
825
	    type = IF_IDE;
826
            max_devs = MAX_IDE_DEVS;
827
        } else if (!strcmp(buf, "scsi")) {
828
	    type = IF_SCSI;
829
            max_devs = MAX_SCSI_DEVS;
830
        } else if (!strcmp(buf, "floppy")) {
831
	    type = IF_FLOPPY;
832
            max_devs = 0;
833
        } else if (!strcmp(buf, "pflash")) {
834
	    type = IF_PFLASH;
835
            max_devs = 0;
836
	} else if (!strcmp(buf, "mtd")) {
837
	    type = IF_MTD;
838
            max_devs = 0;
839
	} else if (!strcmp(buf, "sd")) {
840
	    type = IF_SD;
841
            max_devs = 0;
842
        } else if (!strcmp(buf, "virtio")) {
843
            type = IF_VIRTIO;
844
            max_devs = 0;
845
	} else if (!strcmp(buf, "xen")) {
846
	    type = IF_XEN;
847
            max_devs = 0;
848
	} else if (!strcmp(buf, "none")) {
849
	    type = IF_NONE;
850
            max_devs = 0;
851
	} else {
852
            fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
853
            return NULL;
854
	}
855
    }
856

  
857
    if (cyls || heads || secs) {
858
        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
859
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
860
	    return NULL;
861
	}
862
        if (heads < 1 || (type == IF_IDE && heads > 16)) {
863
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
864
	    return NULL;
865
	}
866
        if (secs < 1 || (type == IF_IDE && secs > 63)) {
867
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
868
	    return NULL;
869
	}
870
    }
871

  
872
    if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
873
        if (!cyls) {
874
            fprintf(stderr,
875
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
876
                    buf);
877
            return NULL;
878
        }
879
        if (!strcmp(buf, "none"))
880
            translation = BIOS_ATA_TRANSLATION_NONE;
881
        else if (!strcmp(buf, "lba"))
882
            translation = BIOS_ATA_TRANSLATION_LBA;
883
        else if (!strcmp(buf, "auto"))
884
            translation = BIOS_ATA_TRANSLATION_AUTO;
885
	else {
886
            fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
887
	    return NULL;
888
	}
889
    }
890

  
891
    if ((buf = qemu_opt_get(opts, "media")) != NULL) {
892
        if (!strcmp(buf, "disk")) {
893
	    media = MEDIA_DISK;
894
	} else if (!strcmp(buf, "cdrom")) {
895
            if (cyls || secs || heads) {
896
                fprintf(stderr,
897
                        "qemu: '%s' invalid physical CHS format\n", buf);
898
	        return NULL;
899
            }
900
	    media = MEDIA_CDROM;
901
	} else {
902
	    fprintf(stderr, "qemu: '%s' invalid media\n", buf);
903
	    return NULL;
904
	}
905
    }
906

  
907
    if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
908
        if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
909
            bdrv_flags |= BDRV_O_NOCACHE;
910
        } else if (!strcmp(buf, "writeback")) {
911
            bdrv_flags |= BDRV_O_CACHE_WB;
912
        } else if (!strcmp(buf, "unsafe")) {
913
            bdrv_flags |= BDRV_O_CACHE_WB;
914
            bdrv_flags |= BDRV_O_NO_FLUSH;
915
        } else if (!strcmp(buf, "writethrough")) {
916
            /* this is the default */
917
        } else {
918
           fprintf(stderr, "qemu: invalid cache option\n");
919
           return NULL;
920
        }
921
    }
922

  
923
#ifdef CONFIG_LINUX_AIO
924
    if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
925
        if (!strcmp(buf, "native")) {
926
            bdrv_flags |= BDRV_O_NATIVE_AIO;
927
        } else if (!strcmp(buf, "threads")) {
928
            /* this is the default */
929
        } else {
930
           fprintf(stderr, "qemu: invalid aio option\n");
931
           return NULL;
932
        }
933
    }
934
#endif
935

  
936
    if ((buf = qemu_opt_get(opts, "format")) != NULL) {
937
       if (strcmp(buf, "?") == 0) {
938
            fprintf(stderr, "qemu: Supported formats:");
939
            bdrv_iterate_format(bdrv_format_print, NULL);
940
            fprintf(stderr, "\n");
941
	    return NULL;
942
        }
943
        drv = bdrv_find_whitelisted_format(buf);
944
        if (!drv) {
945
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
946
            return NULL;
947
        }
948
    }
949

  
950
    on_write_error = BLOCK_ERR_STOP_ENOSPC;
951
    if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
952
        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
953
            fprintf(stderr, "werror is no supported by this format\n");
954
            return NULL;
955
        }
956

  
957
        on_write_error = parse_block_error_action(buf, 0);
958
        if (on_write_error < 0) {
959
            return NULL;
960
        }
961
    }
962

  
963
    on_read_error = BLOCK_ERR_REPORT;
964
    if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
965
        if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
966
            fprintf(stderr, "rerror is no supported by this format\n");
967
            return NULL;
968
        }
969

  
970
        on_read_error = parse_block_error_action(buf, 1);
971
        if (on_read_error < 0) {
972
            return NULL;
973
        }
974
    }
975

  
976
    if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
977
        if (type != IF_VIRTIO) {
978
            fprintf(stderr, "addr is not supported\n");
979
            return NULL;
980
        }
981
    }
982

  
983
    /* compute bus and unit according index */
984

  
985
    if (index != -1) {
986
        if (bus_id != 0 || unit_id != -1) {
987
            fprintf(stderr,
988
                    "qemu: index cannot be used with bus and unit\n");
989
            return NULL;
990
        }
991
        if (max_devs == 0)
992
        {
993
            unit_id = index;
994
            bus_id = 0;
995
        } else {
996
            unit_id = index % max_devs;
997
            bus_id = index / max_devs;
998
        }
999
    }
1000

  
1001
    /* if user doesn't specify a unit_id,
1002
     * try to find the first free
1003
     */
1004

  
1005
    if (unit_id == -1) {
1006
       unit_id = 0;
1007
       while (drive_get(type, bus_id, unit_id) != NULL) {
1008
           unit_id++;
1009
           if (max_devs && unit_id >= max_devs) {
1010
               unit_id -= max_devs;
1011
               bus_id++;
1012
           }
1013
       }
1014
    }
1015

  
1016
    /* check unit id */
1017

  
1018
    if (max_devs && unit_id >= max_devs) {
1019
        fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1020
                unit_id, max_devs - 1);
1021
        return NULL;
1022
    }
1023

  
1024
    /*
1025
     * ignore multiple definitions
1026
     */
1027

  
1028
    if (drive_get(type, bus_id, unit_id) != NULL) {
1029
        *fatal_error = 0;
1030
        return NULL;
1031
    }
1032

  
1033
    /* init */
1034

  
1035
    dinfo = qemu_mallocz(sizeof(*dinfo));
1036
    if ((buf = qemu_opts_id(opts)) != NULL) {
1037
        dinfo->id = qemu_strdup(buf);
1038
    } else {
1039
        /* no id supplied -> create one */
1040
        dinfo->id = qemu_mallocz(32);
1041
        if (type == IF_IDE || type == IF_SCSI)
1042
            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1043
        if (max_devs)
1044
            snprintf(dinfo->id, 32, "%s%i%s%i",
1045
                     devname, bus_id, mediastr, unit_id);
1046
        else
1047
            snprintf(dinfo->id, 32, "%s%s%i",
1048
                     devname, mediastr, unit_id);
1049
    }
1050
    dinfo->bdrv = bdrv_new(dinfo->id);
1051
    dinfo->devaddr = devaddr;
1052
    dinfo->type = type;
1053
    dinfo->bus = bus_id;
1054
    dinfo->unit = unit_id;
1055
    dinfo->on_read_error = on_read_error;
1056
    dinfo->on_write_error = on_write_error;
1057
    dinfo->opts = opts;
1058
    if (serial)
1059
        strncpy(dinfo->serial, serial, sizeof(serial));
1060
    QTAILQ_INSERT_TAIL(&drives, dinfo, next);
1061

  
1062
    switch(type) {
1063
    case IF_IDE:
1064
    case IF_SCSI:
1065
    case IF_XEN:
1066
    case IF_NONE:
1067
        switch(media) {
1068
	case MEDIA_DISK:
1069
            if (cyls != 0) {
1070
                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1071
                bdrv_set_translation_hint(dinfo->bdrv, translation);
1072
            }
1073
	    break;
1074
	case MEDIA_CDROM:
1075
            bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
1076
	    break;
1077
	}
1078
        break;
1079
    case IF_SD:
1080
        /* FIXME: This isn't really a floppy, but it's a reasonable
1081
           approximation.  */
1082
    case IF_FLOPPY:
1083
        bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
1084
        break;
1085
    case IF_PFLASH:
1086
    case IF_MTD:
1087
        break;
1088
    case IF_VIRTIO:
1089
        /* add virtio block device */
1090
        opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1091
        qemu_opt_set(opts, "driver", "virtio-blk-pci");
1092
        qemu_opt_set(opts, "drive", dinfo->id);
1093
        if (devaddr)
1094
            qemu_opt_set(opts, "addr", devaddr);
1095
        break;
1096
    case IF_COUNT:
1097
        abort();
1098
    }
1099
    if (!file) {
1100
        *fatal_error = 0;
1101
        return NULL;
1102
    }
1103
    if (snapshot) {
1104
        /* always use cache=unsafe with snapshot */
1105
        bdrv_flags &= ~BDRV_O_CACHE_MASK;
1106
        bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
1107
    }
1108

  
1109
    if (media == MEDIA_CDROM) {
1110
        /* CDROM is fine for any interface, don't check.  */
1111
        ro = 1;
1112
    } else if (ro == 1) {
1113
        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
1114
            fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
1115
            return NULL;
1116
        }
1117
    }
1118

  
1119
    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
1120

  
1121
    ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
1122
    if (ret < 0) {
1123
        fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1124
                        file, strerror(-ret));
1125
        return NULL;
1126
    }
1127

  
1128
    if (bdrv_key_required(dinfo->bdrv))
1129
        autostart = 0;
1130
    *fatal_error = 0;
1131
    return dinfo;
1132
}
1133

  
1134 653
static int drive_init_func(QemuOpts *opts, void *opaque)
1135 654
{
1136 655
    int *use_scsi = opaque;

Also available in: Unified diff