axvmconfig
路径:
virtualization/axvmconfig类型:库 + 二进制混合 crate 分层:组件层 / 虚拟机配置模型 版本:0.2.2文档依据:当前仓库源码、Cargo.toml、README.md、src/lib.rs、src/tool.rs、virtualization/axvm/src/config.rs与os/axvisor/src/config.rs
axvmconfig 是 Axvisor 虚拟机配置链路的静态模型层。它的职责不是直接创建 VM,也不是直接操作页表或设备,而是把 TOML 中描述的 VM 元信息、镜像布局、内存区域、模拟设备、直通设备和中断模式转换成一组稳定的数据结构;这些结构随后被 axvm 转成运行时 AxVMConfig,再由 os/axvisor 继续执行内存分配、镜像加载、FDT 处理和 VM 实例化。
架构设计
设计定位
axvmconfig 兼具两个身份:
- 作为库 crate,它提供一组
serde数据模型,供 hypervisor 内部解析和传递 VM 配置。 - 作为宿主侧工具,它在开启默认
stdfeature 时提供check/generateCLI,用于校验配置文件和生成模板。
这决定了它的设计有两个显著特征:
- 核心模型保持
no_std兼容,便于axvm、axdevice等运行时 crate 以default-features = false方式依赖。 - 与文件系统、命令行、JSON Schema 生成相关的能力全部通过
stdfeature 外挂。
模块结构
| 模块 | 作用 | 关键内容 |
|---|---|---|
lib.rs | 配置模型主体 | AxVMCrateConfig、VMBaseConfig、VMKernelConfig、VMDevicesConfig、VmMemConfig、EmulatedDeviceType、VMInterruptMode、from_toml() |
tool.rs | CLI 入口逻辑 | check、generate、parse_usize() |
templates.rs | 模板构造 | VmTemplateParams、get_vm_config_template() |
main.rs | 可执行入口 | 初始化 env_logger,调用工具子命令 |
templates/*.toml | 静态样例 | 宿主侧示例模板资源 |
虽然库的代码量不小,但主要语义都集中在 lib.rs 里,整体更像“配置模型仓库”而不是多模块运行时系统。
1.3 配置结构体系
AxVMCrateConfig 是最上层根结构,拆分为三大部分:
base: VMBaseConfigkernel: VMKernelConfigdevices: VMDevicesConfig
这三个部分分别对应“VM 身份与 CPU 资源”“镜像与内存布局”“设备与中断策略”。
VMBaseConfig
该结构描述 VM 的基本属性和宿主 CPU 绑定信息:
id、namevm_typecpu_numphys_cpu_idsphys_cpu_sets
值得注意的是,vm_type 在此层仍是 usize,而不是 VMType 枚举。这说明解析层刻意保持输入宽容,后续再由 AxVMConfig::from() 转换成枚举语义。
VMKernelConfig
这一层描述镜像装载与内存布局:
entry_pointkernel_path/kernel_load_addrboot_protocolbios_path/bios_load_addrdtb_path/dtb_load_addrramdisk_path/ramdisk_load_addrimage_locationcmdlinedisk_pathmemory_regions: Vec<VmMemConfig>
boot_protocol describes how the guest enters its boot image. It defaults to direct when enable_bios = false, and keeps the historical multiboot behavior when enable_bios = true and the field is omitted. x86_64 guests can set boot_protocol = "uefi" to load an external UEFI firmware image without applying the legacy axvm-bios multiboot patch.
VmMemConfig 则把每段 guest 物理内存刻画为:
gpasizeflagsmap_type
其中 map_type 是 VmMemMappingType,当前有三类:
MapAllocMapIdenticalMapReserved
VMDevicesConfig
设备配置层包含:
emu_devicespassthrough_devicesinterrupt_modeexcluded_devicespassthrough_addresses
这使 axvmconfig 不只是“镜像配置文件”,而是完整的 VM 资源声明格式。