怎样进行qemu中KVM硬件虚拟化的初始化分析
怎样进行qemu中KVM硬件虚拟化的初始化分析 ,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
房县网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联公司于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
qemu中KVM硬件虚拟化的初始化分析 2014-04-25 14:38:38
分类: 虚拟化
1.传入-enable-kvm参数,启用KVM硬件虚拟化支持:
在文件qemu-options.hx中,有对参数-enable-kvm的定义,如下:
DEF("enable-kvm", 0, QEMU_OPTION_enable_kvm, \
"-enable-kvm enable KVM full virtualization support\n", QEMU_ARCH_ALL)
STEXI
@item -enable-kvm
@findex -enable-kvm
Enable KVM full virtualization support. This option is only available
if KVM support is enabled when compiling.
ETEXI
2.main函数中对应的参数解析
int main(int argc, char **argv, char **envp)
{
/* second pass of option parsing */
optind = 1;
for(;;) {
......
case QEMU_OPTION_enable_kvm:
olist = qemu_find_opts("machine");
qemu_opts_parse(olist, "accel=kvm", 0);
break;
......
}
......
os_daemonize();
......
configure_accelerator(machine);
......
}
(gdb) p *machine
$46 = {name = 0x555555a17666 "pc-i440fx-2.0", alias = 0x555555a17674 "pc",
desc = 0x555555a175d8 "Standard PC (i440FX + PIIX, 1996)", init = 0x5555558c2e2c
hot_add_cpu = 0x5555558c1061
no_parallel = 0, use_virtcon = 0, use_sclp = 0, no_floppy = 0, no_cdrom = 0, no_sdcard = 0, is_default = 1,
default_machine_opts = 0x555555a17677 "firmware=bios-256k.bin", default_boot_order = 0x555555a1768e "cad", compat_props = 0x0,
next = 0x0, hw_version = 0x0}
//qemu支持4种方式执行:tcg、xen、kvm、qtest
static struct {
const char *opt_name;
const char *name;
int (*available)(void);
int (*init)(QEMUMachine *);
bool *allowed;
} accel_list[] = {
{ "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
{ "xen", "Xen", xen_available, xen_init, &xen_allowed },
{ "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
{ "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
};
//解析参数,并初始化相关模块
static int configure_accelerator(QEMUMachine *machine)
{
const char *p;
char buf[10];
int i, ret;
bool accel_initialised = false;
bool init_failed = false;
//在没有带-enable-kvm参数时,默认采用软件仿真的tcg
p = qemu_opt_get(qemu_get_machine_opts(), "accel");
if (p == NULL) {
/* Use the default "accelerator", tcg */
p = "tcg";
}
while (!accel_initialised && *p != '\0') {
if (*p == ':') {
p++;
}
p = get_opt_name(buf, sizeof (buf), p, ':');
for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
if (strcmp(accel_list[i].opt_name, buf) == 0) {
if (!accel_list[i].available()) {
printf("%s not supported for this target\n",
accel_list[i].name);
continue;
}
*(accel_list[i].allowed) = true;
ret = accel_list[i].init(machine);
if (ret < 0) {
init_failed = true;
fprintf(stderr, "failed to initialize %s: %s\n",
accel_list[i].name,
strerror(-ret));
*(accel_list[i].allowed) = false;
} else {
accel_initialised = true;
}
break;
}
}
if (i == ARRAY_SIZE(accel_list)) {
fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
}
}
if (!accel_initialised) {
if (!init_failed) {
fprintf(stderr, "No accelerator found!\n");
}
exit(1);
}
if (init_failed) {
fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
}
return !accel_initialised;
}
3.kvm_init
int kvm_init(QEMUMachine *machine)
{
......
s->fd = qemu_open("/dev/kvm", O_RDWR);
if (s->fd == -1) {
fprintf(stderr, "Could not access KVM kernel module: %m\n");
ret = -errno;
goto err;
}
//匹配kvm内核模块的版本,版本太旧或者太新都会不支持
ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
if (ret < KVM_API_VERSION) {
if (ret > 0) {
ret = -EINVAL;
}
fprintf(stderr, "kvm version too old\n");
goto err;
}
if (ret > KVM_API_VERSION) {
ret = -EINVAL;
fprintf(stderr, "kvm version not supported\n");
goto err;
}
......
/* check the vcpu limits */
soft_vcpus_limit = kvm_recommended_vcpus(s); //获取kvm内核推荐使用的vcpu个数
hard_vcpus_limit = kvm_max_vcpus(s); //获取kvm内核最多可用的vcpu个数
......
do {
ret = kvm_ioctl(s, KVM_CREATE_VM, type); //创建一个VM
} while (ret == -EINTR);
......
ret = kvm_arch_init(s);
if (ret < 0) {
goto err;
}
ret = kvm_irqchip_create(s);
if (ret < 0) {
goto err;
}
kvm_state = s;
memory_listener_register(&kvm_memory_listener, &address_space_memory);
memory_listener_register(&kvm_io_listener, &address_space_io);
s->many_ioeventfds = kvm_check_many_ioeventfds();
cpu_interrupt_handler = kvm_handle_interrupt;
return 0;
......
}
看完上述内容,你们掌握怎样进行qemu中KVM硬件虚拟化的初始化分析 的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网站栏目:怎样进行qemu中KVM硬件虚拟化的初始化分析
网页地址:http://azwzsj.com/article/ijcddi.html