zoukankan      html  css  js  c++  java
  • KVM 虚机的创建过程

     https://www.cnblogs.com/liujunjun/p/12444772.html

    In contrast to container, the concept Virtual Machine represents another category of isolated execution environment. Considering that most of VMs leverages some hardware-accelerating techniques like VT-x, I will assume you are talking about hardware-assisted virtualization. Virtual Machine usually contains a full kernel inside it.

    See this picture from Doug Chamberlain enter image description here

    The Intel VT-x technique provides 2 modes, root mode(privileged) and non-root mode(not privileged). Each mode has its own ring0-ring3 (e.g, non-root ring3, non-root ring0, root ring3, root ring 0). The whole virtual machine runs in non-root mode, and the hypervisor(VMM, e.g., kvm) runs in root-mode.

    In the classic qemu+kvm setup, qemu runs in root ring3, and kvm runs in root ring0.

    The strong isolation and the existance of guest kernel makes virtual machine more secure and compatible. But, of course, the price is performance and efficiency (slower boot etc.)

     

     

    KVM启动环境概述

    支持虚拟化的 CPU 中都增加了新的功能。以 Intel VT 技术为例,它增加了两种运行模式:VMX root 模式和 VMX nonroot 模式。通常来讲,主机操作系统和 VMM 运行在 VMX root 模式中,客户机操作系统及其应用运行在 VMX nonroot 模式中。因为两个模式都支持所有的 ring,因此,客户机可以运行在它所需要的 ring 中(OS 运行在 ring 0 中,应用运行在 ring 3 中),VMM 也运行在其需要的 ring 中 (对 KVM 来说,QEMU 运行在 ring 3,KVM 运行在 ring 0)。
    CPU 在两种模式之间的切换称为 VMX 切换。从 root mode 进入 nonroot mode,称为 VM entry;从 nonroot mode 进入 root mode,称为 VM exit。可见,CPU 受控制地在两种模式之间切换,轮流执行 VMM 代码和 Guest OS 代码。
    对 KVM 虚机来说,运行在 VMX Root Mode 下的 VMM 在需要执行 Guest OS 指令时执行 VMLAUNCH 指令将 CPU 转换到 VMX non-root mode,开始执行客户机代码,即 VM entry 过程;在 Guest OS 需要退出该 mode 时,CPU 自动切换到 VMX Root mode,即 VM exit 过程。可见,KVM 客户机代码是受 VMM 控制直接运行在物理 CPU 上的。QEMU 只是通过 KVM 控制虚机的代码被 CPU 执行,但是它们本身并不执行其代码。也就是说,CPU 并没有真正的被虚级化成虚拟的 CPU 给客户机使用。

    image.png | center | 700x741.125

    由上可见 :

    1. qemu-kvm 通过对 /dev/kvm 的 一系列 ICOTL 命令控制虚机
    2. 一个 KVM 虚机即一个 Linux qemu-kvm 进程,与其他 Linux 进程一样被Linux 进程调度器调度。
    3. KVM 虚机包括虚拟内存、虚拟CPU和虚机 I/O设备,其中,内存和 CPU 的虚拟化由 KVM 内核模块负责实现,I/O 设备的虚拟化由 QEMU 负责实现。
    4. KVM户机系统的内存是 qemu-kvm 进程的地址空间的一部分。
    5. KVM 虚机的 vCPU 作为 线程运行在 qemu-kvm 进程的上下文中。



     VM中代码是如何运行

    一个普通的 Linux 内核有两种执行模式:内核模式(Kenerl)和用户模式 (User)。
    为了支持带有虚拟化功能的 CPU,KVM 向 Linux 内核增加了第三种模式即客户机模式(Guest),该模式对应于 CPU 的 VMX non-root mode。

     KVM kernel mode

    KVM 内核模块作为 User mode 和 Guest mode 之间的桥梁:

    • User mode 中的 QEMU-KVM 会通过 ICOTL 命令来运行虚拟机
    • KVM 内核模块收到该请求后,它先做一些准备工作,比如将 VCPU 上下文加载到 VMCS (virtual machine control structure)等,然后驱动 CPU 进入 VMX non-root 模式,开始执行客户机代码
      三种模式的分工为:
    • Guest 模式:执行客户机系统非 I/O 代码,并在需要的时候驱动 CPU 退出该模式
    • Kernel 模式:负责将 CPU 切换到 Guest mode 执行 Guest OS 代码,并在 CPU 退出 Guest mode 时回到 Kenerl 模式
    • User 模式:代表客户机系统执行 I/O 操作

    image.png | center | 680x512.5073746312685

    KVM processing

    QEMU-KVM 相比原生 QEMU 的改动:

    • 原生的 QEMU 通过指令翻译实现 CPU 的完全虚拟化,但是修改后的 QEMU-KVM 会调用 ICOTL 命令来调用 KVM 模块。
    • 原生的 QEMU 是单线程实现,QEMU-KVM 是多线程实现。
      主机 Linux 将一个虚拟视作一个 QEMU 进程,该进程包括下面几种线程:
    • I/O 线程用于管理模拟设备 vCPU 线程用于运行 Guest 代码
    • 其它线程,比如处理 event loop,offloaded tasks 等的线程

    image.png | center | 680x507.3475177304964

  • 相关阅读:
    Java程序员必知的8大排序
    Servlet与JSP间的传值问题
    Servlet上传文件详细解析以及注意事项
    JSP入门学习经验
    统计语言模型(Statistical Language Models)(条件概率公式)
    自定义用户控件ascx
    asp.net相对路径、绝对路径
    A list of supported CSS selectors when invoking g(..)
    一位研究生导师心目中理想的论文(转)
    常用软件
  • 原文地址:https://www.cnblogs.com/dream397/p/14260900.html
Copyright © 2011-2022 走看看