您好,登錄后才能下訂單哦!
怎樣進行qemu中KVM硬件虛擬化的初始化分析 ,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
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 <pc_init_pci>, reset = 0,
hot_add_cpu = 0x5555558c1061 <pc_hot_add_cpu>, kvm_type = 0, block_default_type = IF_IDE, max_cpus = 255, no_serial = 0,
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硬件虛擬化的初始化分析 的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。