- 云原生
- 容器运行时
【免费下载链接】kata-containers
Kata Containers is an open source project and community working to build a standard implementation of lightweight Virtual Machines (VMs) that feel and perform like containers, but provide the workload isolation and security advantages of VMs. https://katacontainers.io/
Kata Containers 在src/runtime/virtcontainers/pkg/cloud-hypervisor/client目录下维护着一套由 OpenAPI 规范自动生成的 Go 客户端,用于通过本地 HTTP API 管理 Cloud Hypervisor 虚拟机。其中TpmConfig是VmConfig中用于描述 TPM(可信平台模块)设备连接参数的核心模型。本文基于 TpmConfig.md 展开,结合仓库内的 OpenAPI 定义、模型源码与 virtcontainers 中真实的 TPM 设备接入实现,说明 TpmConfig 的字段语义、构造函数与访问器用法,以及它在整条 TPM 直通链路中的位置。
TpmConfig 模型概述
TpmConfig是 Cloud Hypervisor API(版本 0.3.0)中用于配置 TPM 设备的配置结构体。在 Kata Containers 的 Go 客户端中,它定义于 model_tpm_config.go,完整结构体如下:
// TpmConfig struct for TpmConfig type TpmConfig struct { Socket string `json:"socket"` }该结构体只有一个字段,其属性定义如下表(与原文档一致):
| 属性名 | 类型 | 说明 | 是否必填 |
|---|---|---|---|
| Socket | string | TPM 设备通信使用的 socket 路径(如 vTPM 的 Unix socket 地址) | 是 |
从 OpenAPI 规范(openapi.yaml)可以看到该 schema 的完整定义,其中socket被标记为required,意味着构造一个合法的TpmConfig必须提供 socket 路径:
TpmConfig: example: socket: socket properties: socket: type: string required: - socket type: object需要注意的是,这个Socket字段指向的是宿主机上供 VMM 与 TPM 设备通信的 socket 路径,而不是 guest 内部路径。Cloud Hypervisor 通过该 socket 将虚拟化 TPM(vTPM)暴露给 guest,Kata 运行时在构造 VmConfig 时把该路径透传给 VMM。
TpmConfig 在 VmConfig 中的位置
TpmConfig并不是独立使用的模型,而是作为 VmConfig 的tpm字段存在。在 model_vm_config.go 中:
type VmConfig struct { // ... Tpm *TpmConfig `json:"tpm,omitempty"` // ... }对应 OpenAPI 中VmConfig的tpm属性引用(openapi.yaml):
tpm: $ref: '#/components/schemas/TpmConfig'这意味着创建 VM 时,如果希望启用 TPM 设备,需要构造一个TpmConfig实例并通过SetTpm注入到VmConfig中(model_vm_config.go):
// HasTpm returns a boolean if a field has been set. func (o *VmConfig) HasTpm() bool { if o != nil && o.Tpm != nil { return true } return false } // SetTpm gets a reference to the given TpmConfig and assigns it to the Tpm field. func (o *VmConfig) SetTpm(v TpmConfig) { o.Tpm = &v }在 JSON 序列化时,VmConfig只有在Tpm字段非 nil 时才会输出tpm键(model_vm_config.go):
if o.Tpm != nil { toSerialize["tpm"] = o.Tpm }构造函数与字段访问方法
客户端为TpmConfig生成了两类构造函数和一组字段访问方法,原文档中给出的方法签名全部继承如下。
NewTpmConfig:完整构造函数
func NewTpmConfig(socket string) *TpmConfigNewTpmConfig实例化一个新的TpmConfig对象。该构造函数会为定义过默认值的属性赋默认值,并确保 API 要求的必填属性被设置(即socket)。其源码实现(model_tpm_config.go):
func NewTpmConfig(socket string) *TpmConfig { this := TpmConfig{} this.Socket = socket return &this }由于TpmConfig只有一个必填字段socket,因此该构造函数直接接受 socket 路径作为唯一参数。
NewTpmConfigWithDefaults:默认构造函数
func NewTpmConfigWithDefaults() *TpmConfigNewTpmConfigWithDefaults同样实例化一个新的TpmConfig对象,但只对定义过默认值的属性赋默认值,不保证必填属性(socket)被设置(model_tpm_config.go):
func NewTpmConfigWithDefaults() *TpmConfig { this := TpmConfig{} return &this }由于socket没有默认值,调用该构造函数得到的对象Socket为空字符串。使用该构造函数后必须手动调用SetSocket,否则序列化出的配置缺少有效的 socket 路径,会导致 VM 创建失败或 TPM 设备无法连接。
字段访问方法
原文档列出的三个访问方法及其源码实现如下:
| 方法 | 签名 | 行为 |
|---|---|---|
GetSocket | func (o *TpmConfig) GetSocket() string | 返回Socket字段;若接收者为 nil,返回零值(空字符串) |
GetSocketOk | func (o *TpmConfig) GetSocketOk() (*string, bool) | 返回Socket字段指针与布尔值;若接收者为 nil,返回(nil, false) |
SetSocket | func (o *TpmConfig) SetSocket(v string) | 将Socket字段设置为给定值 |
对应实现(model_tpm_config.go):
// GetSocket returns the Socket field value func (o *TpmConfig) GetSocket() string { if o == nil { var ret string return ret } return o.Socket } // GetSocketOk returns a tuple with the Socket field value // and a boolean to check if the value has been set. func (o *TpmConfig) GetSocketOk() (*string, bool) { if o == nil { return nil, false } return &o.Socket, true } // SetSocket sets field value func (o *TpmConfig) SetSocket(v string) { o.Socket = v }需要注意GetSocketOk的语义:只要接收者非 nil,它返回的布尔值恒为true——该布尔值表示的是对象本身是否可访问,而非字段是否被显式赋值。要判断字段是否设置,应使用VmConfig层面的HasTpm()(见上文)或检查 socket 字符串是否为空。
序列化与可空包装类型
客户端还提供了 JSON 序列化方法与可空包装类型,虽然原文档未逐一列出,但它们是模型完整使用方式的一部分。
MarshalJSON 实现
TpmConfig的MarshalJSON(model_tpm_config.go)将Socket字段序列化为 JSON 键socket:
func (o TpmConfig) MarshalJSON() ([]byte, error) { toSerialize := map[string]interface{}{} if true { toSerialize["socket"] = o.Socket } return json.Marshal(toSerialize) }因此一个合法的 TpmConfig JSON 表示形如:
{ "socket": "/path/to/tpm-socket" }NullableTpmConfig 可空包装
针对 TPM 配置可能为"未设置"状态(nil)的场景,客户端生成了NullableTpmConfig包装类型(model_tpm_config.go),提供Get、Set、IsSet、Unset、NewNullableTpmConfig以及对应的MarshalJSON/UnmarshalJSON方法。它允许调用方在"值不存在"与"值为零值"之间做出区分:
type NullableTpmConfig struct { value *TpmConfig isSet bool } func NewNullableTpmConfig(val *TpmConfig) *NullableTpmConfig { return &NullableTpmConfig{value: val, isSet: true} }实战用法:构造带 TPM 的 VmConfig
综合上述 API,在 Kata Containers 的 Cloud Hypervisor 客户端中,一个启用 TPM 设备的 VM 配置构造流程如下:
import ( openapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/cloud-hypervisor/client" ) // 方式一:使用完整构造函数(推荐,socket 必填) tpm := openapi.NewTpmConfig("/var/run/vtpm.sock") // 方式二:使用默认构造函数后手动设置 tpm2 := openapi.NewTpmConfigWithDefaults() tpm2.SetSocket("/var/run/vtpm.sock") // 校验 socket 是否已设置 if socket, ok := tpm2.GetSocketOk(); ok && *socket != "" { // socket 已就绪 } // 注入 VmConfig vmConfig := openapi.NewVmConfig(payload) vmConfig.SetTpm(*tpm) if vmConfig.HasTpm() { // 序列化后将包含 "tpm" 键 raw, _ := vmConfig.MarshalJSON() }生成的 JSON 中会包含:
{ "payload": { ... }, "tpm": { "socket": "/var/run/vtpm.sock" } }仓库中的 TPM 设备接入佐证
虽然 Cloud Hypervisor 客户端将TpmConfig作为VmConfig的tpm字段暴露,但 Kata Containers 的 virtcontainers 层在 QEMU 后端同样实现了 TPM 设备直通,可作为理解该配置用途的旁证。在 qemu_ppc64le.go 中定义了 TPM 设备的常量:
const tpmID = "tpm0" const tpmHostPath = "/dev/tpmrm0"在设备追加逻辑中(qemu_ppc64le.go),宿主机 TPM 资源/dev/tpmrm0会被映射为 QEMU 设备tpm0:
DeviceID: tpmID, File: tpmHostPath,对应的测试用例 qemu_ppc64le_test.go 验证了该映射关系。由此可见,无论是 QEMU 后端通过设备直通、还是 Cloud Hypervisor 后端通过TpmConfig.Socket指向的 vTPM socket,Kata Containers 的 TPM 支持最终都落在"将宿主机 TPM 资源暴露给 guest"这一目标上,TpmConfig正是 Cloud Hypervisor 侧完成这一目标所需的唯一配置入口。
小结
TpmConfig是 Kata Containers 中 Cloud Hypervisor OpenAPI 客户端(API 版本 0.3.0)的 TPM 设备配置模型,仅包含必填的Socket字段,定义于 model_tpm_config.go。- 它作为
VmConfig.Tpm(*TpmConfig,omitempty)字段使用,通过SetTpm/HasTpm注入与检测,序列化结果为{"socket": "<path>"}。 - 推荐使用
NewTpmConfig(socket)构造;NewTpmConfigWithDefaults()不会设置 socket,必须配合SetSocket使用。 - 该模型对应 Cloud Hypervisor 的 vTPM socket 接入方式,与 virtcontainers 中 QEMU 后端经
/dev/tpmrm0直通 TPM(见 qemu_ppc64le.go)共同构成 Kata 运行时对 TPM 设备的支持路径。
如需查看完整的客户端模型列表与 API 端点,可继续阅读 client README 与 VmConfig 文档。
- 云原生
- 容器运行时
【免费下载链接】kata-containers
Kata Containers is an open source project and community working to build a standard implementation of lightweight Virtual Machines (VMs) that feel and perform like containers, but provide the workload isolation and security advantages of VMs. https://katacontainers.io/
相关推荐
Agentic 发布部署后如何用 cURL 通过 MCP Gateway HTTP 端点调用工具
Agentic 发布部署后如何用 cURL 通过 MCP Gateway HTTP 端点调用工具 把一个 MCP server(或 OpenAPI servic
云原生容器运行时Kata Containers 多 Hypervisor 技术解析:QEMU、Cloud Hypervisor、Firecracker、Dragonball 与 StratoVirt 选型与配置详解
Kata Containers 多 Hypervisor 技术解析:QEMU、Cloud Hypervisor、Firecracker、Dragonball 与
云原生容器运行时Kubo 的 /wss 出站拨号与 HTTP 请求如何用 HTTPS_PROXY 和 NO_PROXY 走代理?
Kubo 的 /wss 出站拨号与 HTTP 请求如何用 HTTPS_PROXY 和 NO_PROXY 走代理? 如果你的 Kubo 节点部署在只能通过代理访问
云原生容器运行时
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考