Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222223
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 513
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-23 00:06
个人简介

展示自己、证明自己

文章分类

全部博文(76)

文章存档

2018年(1)

2014年(55)

2013年(20)

我的朋友

分类: C/C++

2014-08-04 10:36:59

1.注册映像,设备,机器相应数据结构
block_init()
device_init()
machine_init()
注意:
    上述函数为构造函数__attribute__((constructor)),即在main()函数执行前,相关函数已执行。

#define module_init(function, type)                                         \
static void __attribute__((constructor)) do_qemu_init_ ## function(void) {  \
    register_module_init(function, type);                                   \
}   
    
typedef enum {
    MODULE_INIT_BLOCK,
    MODULE_INIT_DEVICE,
    MODULE_INIT_MACHINE,
    MODULE_INIT_MAX
} module_init_type;
    
#define block_init(function) module_init(function, MODULE_INIT_BLOCK)
#define device_init(function) module_init(function, MODULE_INIT_DEVICE)
#define machine_init(function) module_init(function, MODULE_INIT_MACHINE)

2.调用注册的映像,设备,机器相应数据结构初始化函数---完成block,设备,机器的初始化
block.c: module_call_init(MODULE_INIT_BLOCK);
vl.c:    module_call_init(MODULE_INIT_MACHINE);
vl.c:    module_call_init(MODULE_INIT_DEVICE);

3.加载虚拟机驱动程序---命令行参数指定驱动名称
-drive file=/var/lib/libvirt/images/ha1.img,if=none,id=drive-ide0-0-0,format=raw,cache=none

main()
{
    if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
        exit(1);

}

static int drive_init_func(QemuOpts *opts, void *opaque)
{
    int *use_scsi = opaque;

    return drive_init(opts, *use_scsi) == NULL;
}


DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
{
 file = qemu_opt_get(opts, "file");

 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
        pstrcpy(devname, sizeof(devname), buf);
        for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
            ;
        if (type == IF_COUNT) {
            error_report("unsupported bus type '%s'", buf);
            return NULL;
        }
    }

if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
        if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
            bdrv_flags |= BDRV_O_NOCACHE;
        } else if (!strcmp(buf, "writeback")) {
            bdrv_flags |= BDRV_O_CACHE_WB;
        } else if (!strcmp(buf, "unsafe")) {
            bdrv_flags |= BDRV_O_CACHE_WB;
            bdrv_flags |= BDRV_O_NO_FLUSH;
        } else if (!strcmp(buf, "writethrough")) {
            /* this is the default */
        } else {
           error_report("invalid cache option");
           return NULL;
        }
    }
  if ((buf = qemu_opt_get(opts, "format")) != NULL) {
       if (strcmp(buf, "?") == 0) {
           error_printf("Supported formats:");
           bdrv_iterate_format(bdrv_format_print, NULL);
           error_printf("\n");
           return NULL;
        }
        drv = bdrv_find_whitelisted_format(buf);
        if (!drv) {
            error_report("'%s' invalid format", buf);
            return NULL;
        }
    }

    dinfo = qemu_mallocz(sizeof(*dinfo));
    if ((buf = qemu_opts_id(opts)) != NULL) {
        dinfo->id = qemu_strdup(buf);
    } else {
        /* no id supplied -> create one */
        dinfo->id = qemu_mallocz(32);
        if (type == IF_IDE || type == IF_SCSI)
            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
        if (max_devs)
            snprintf(dinfo->id, 32, "%s%i%s%i",
                     devname, bus_id, mediastr, unit_id);
        else
            snprintf(dinfo->id, 32, "%s%s%i",
                     devname, mediastr, unit_id);
    }
    dinfo->bdrv = bdrv_new(dinfo->id);

    if (drive_open(dinfo) < 0) {
        goto err;
    }

}

/* create a new block device (by default it is empty) */
BlockDriverState *bdrv_new(const char *device_name)
{           
    BlockDriverState *bs;
        
    bs = qemu_mallocz(sizeof(BlockDriverState));
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
    if (device_name[0] != '\0') {
        QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
    }
    return bs;
}

static int drive_open(DriveInfo *dinfo)
{
    int res;
    int bdrv_flags = dinfo->bdrv_flags;

    if (runstate_check(RUN_STATE_INMIGRATE)) {
        bdrv_flags |= BDRV_O_INCOMING;
    }

    res = bdrv_open(dinfo->bdrv, dinfo->file, bdrv_flags, dinfo->drv);

    if (res < 0) {
        error_report("could not open disk image %s: %s",
                     dinfo->file, strerror(-res));
    }
    return res;
}

int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
              BlockDriver *drv)
{
 /* 获取虚拟机映像的驱动,例如 raw, qcow2, vmdk, ...*/
    if (!drv) {
        ret = find_image_format(filename, &drv);
    }

    if (!drv) {
        goto unlink_and_fail;
    }

    /* Open the image */
    ret = bdrv_open_common(bs, filename, flags, drv);
    if (ret < 0) {
        goto unlink_and_fail;
    }
}

4. 加载虚拟机设备---命令行参数指定设备名称
-device ide-drive,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1
上面步骤2意思是虚拟机支持的设备,本步骤说明是虚拟机目前已添加设备
注意:
    往下分析,会发现有调用设备初始化函数rc = dev->info->init(dev, dev->info); //,不是上面已经调用,并完成初始化。此初始化功能非上面初始化功能。
此初始化为真正初始化功能,主要功能是把相应设备放入相应总线上。上面初始化函数目的是分配与配置相应寄存器和端口。
    
main()
{
    if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
        exit(1);
}

static int device_init_func(QemuOpts *opts, void *opaque)
{
    DeviceState *dev;

    dev = qdev_device_add(opts);
    if (!dev)
        return -1;
    return 0;
}

DeviceState *qdev_device_add(QemuOpts *opts)
{
    const char *driver, *path, *id;
    DeviceInfo *info;
    DeviceState *qdev;
    BusState *bus;

    driver = qemu_opt_get(opts, "driver");
    if (!driver) {
        qerror_report(QERR_MISSING_PARAMETER, "driver");
        return NULL;
    }

    /* find driver */
    info = qdev_find_info(NULL, driver);
    if (!info || info->no_user) {
        qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "a driver name");
        error_printf_unless_qmp("Try with argument '?' for a list.\n");
        return NULL;
    }

    /* find bus */
if (path != NULL) {
        bus = qbus_find(path);
        if (!bus) {
            return NULL;
        }
        if (bus->info != info->bus_info) {
            qerror_report(QERR_BAD_BUS_FOR_DEVICE,
                           driver, bus->info->name);
            return NULL;
        }
    } else {
        bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
        if (!bus) {
            qerror_report(QERR_NO_BUS_FOR_DEVICE,
                           info->name, info->bus_info->name);
            return NULL;
        }
    }
    if (qdev_hotplug && !bus->allow_hotplug) {
        qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
        return NULL;
    }

    /* create device, set properties */
    qdev = qdev_create_from_info(bus, info);
    id = qemu_opts_id(opts);
    if (id) {
     qdev->id = id;
    }
    if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
        qdev_free(qdev);
        return NULL;
    }
    if (qdev_init(qdev) < 0) {
        qerror_report(QERR_DEVICE_INIT_FAILED, driver);
        return NULL;
    }
    qdev->opts = opts;
    return qdev;
}

int qdev_init(DeviceState *dev)
{
    int rc;

    assert(dev->state == DEV_STATE_CREATED);
    rc = dev->info->init(dev, dev->info); //注意上面初始化区别
    if (rc < 0) {
        qdev_free(dev);
        return rc;
    }
    if (dev->hotplugged) {
        qdev_reset(dev);
    }
    qemu_register_reset(qdev_reset, dev);
    if (dev->info->vmsd)
        vmstate_register(dev, -1, dev->info->vmsd, dev);
    dev->state = DEV_STATE_INITIALIZED;
    return 0;
}

http://blog.csdn.net/zhuriyuxiao/article/details/9232951
阅读(1304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~