diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index 48956a8b..b4840138 100644 --- a/proxmox/config_qemu.go +++ b/proxmox/config_qemu.go @@ -88,8 +88,9 @@ type ConfigQemu struct { Sshkeys string `json:"sshkeys,omitempty"` // TODO should be an array of strings Startup string `json:"startup,omitempty"` // TODO should be a struct? Tablet *bool `json:"tablet,omitempty"` - Tags string `json:"tags,omitempty"` // TODO should be an array of a custom type as there are character and length limitations - VmID int `json:"vmid,omitempty"` // TODO should be a custom type as there are limitations + Tags string `json:"tags,omitempty"` // TODO should be an array of a custom type as there are character and length limitations + TPMState QemuDevice `json:"tpmstate,omitempty"` // TODO should be a struct + VmID int `json:"vmid,omitempty"` // TODO should be a custom type as there are limitations } const ( @@ -182,6 +183,9 @@ func (config ConfigQemu) CreateVm(vmr *VmRef, client *Client) (err error) { // Create EFI disk config.CreateQemuEfiParams(params) + // Create TPM State + config.CreateQemuTpmParams(params) + // Create VirtIO RNG config.CreateQemuRngParams(params) @@ -289,6 +293,9 @@ func (config *ConfigQemu) defaults() { if config.Tablet == nil { config.Tablet = util.Pointer(true) } + if config.TPMState == nil { + config.TPMState = QemuDevice{} + } } @@ -425,6 +432,9 @@ func (config ConfigQemu) mapToApiValues(currentConfig ConfigQemu) (rebootRequire // Create EFI disk config.CreateQemuEfiParams(params) + // Create TPM state + config.CreateQemuTpmParams(params) + // Create VirtIO RNG config.CreateQemuRngParams(params) @@ -816,6 +826,15 @@ func (ConfigQemu) mapToStruct(vmr *VmRef, params map[string]interface{}) (*Confi config.EFIDisk = efiDiskConfMap } + // tpmstate + if tpmstate, isSet := params["tpmstate0"].(string); isSet { + tpmStateConfMap := ParsePMConf(tpmstate, "volume") + storageName, fileName := ParseSubConf(tpmStateConfMap["volume"].(string), ":") + tpmStateConfMap["storage"] = storageName + tpmStateConfMap["file"] = fileName + config.TPMState = tpmStateConfMap + } + return &config, nil } @@ -1733,6 +1752,30 @@ func (p QemuDeviceParam) createDeviceParam( return p } +// Create tpm parameter. +func (c ConfigQemu) CreateQemuTpmParams(params map[string]interface{}) { + tpmParam := QemuDeviceParam{} + tpmParam = tpmParam.createDeviceParam(c.TPMState, nil) + + if len(tpmParam) > 0 { + tpm_info := []string{} + tpm := "" + for _, param := range tpmParam { + key := strings.Split(param, "=") + if key[0] == "storage" { + // Proxmox format for disk creation + tpm = fmt.Sprintf("%s:1", key[1]) + } else { + tpm_info = append(tpm_info, param) + } + } + if len(tpm_info) > 0 { + tpm = fmt.Sprintf("%s,%s", tpm, strings.Join(tpm_info, ",")) + } + params["tpmstate0"] = tpm + } +} + // readDeviceConfig - get standard sub-conf strings where `key=value` and update conf map. func (confMap QemuDevice) readDeviceConfig(confList []string) { // Add device config. diff --git a/proxmox/config_qemu_test.go b/proxmox/config_qemu_test.go index 886643fe..8b617f58 100644 --- a/proxmox/config_qemu_test.go +++ b/proxmox/config_qemu_test.go @@ -5763,6 +5763,17 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { "volume": "local-lvm:vm-1000-disk-0", }}, }, + // TPM + {name: "TPM State", + input: map[string]interface{}{"tpmstate0": "local-lvm:vm-1000-disk-1.raw,size=4M,version=v2.0"}, + output: &ConfigQemu{TPMState: map[string]interface{}{ + "file": "vm-1000-disk-1.raw", + "size": "4M", + "storage": "local-lvm", + "version": "v2.0", + "volume": "local-lvm:vm-1000-disk-1.raw", + }}, + }, // Node {name: "Node vmr nil", output: &ConfigQemu{},