From 34dfe60bb8e507daabf3b525516c9ca554823ff2 Mon Sep 17 00:00:00 2001 From: Martin Pywell Date: Wed, 13 Mar 2024 06:43:22 +0000 Subject: [PATCH 1/3] add TPM --- proxmox/config_qemu.go | 47 +++++++++++++++++++++++++++++++++++-- proxmox/config_qemu_test.go | 11 +++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index 48956a8b..377f6afe 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 ( @@ -219,6 +220,9 @@ func (config ConfigQemu) CreateVm(vmr *VmRef, client *Client) (err error) { return fmt.Errorf("[ERROR] %q", err) } + // Create TPM State disk + config.CreateQemuTpmParams(params) + return } @@ -289,6 +293,9 @@ func (config *ConfigQemu) defaults() { if config.Tablet == nil { config.Tablet = util.Pointer(true) } + if config.TPMState == nil { + config.TPMState = QemuDevice{} + } } @@ -445,6 +452,9 @@ func (config ConfigQemu) mapToApiValues(currentConfig ConfigQemu) (rebootRequire config.CreateQemuPCIsParams(params) + // Create TPM state disk + config.CreateQemuTpmParams(params) + err = config.CreateIpconfigParams(params) if err != nil { log.Printf("[ERROR] %q", err) @@ -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 { + storage_info := []string{} + storage := "" + for _, param := range tpmParam { + key := strings.Split(param, "=") + if key[0] == "storage" { + // Proxmox format for disk creation + storage = fmt.Sprintf("%s:1", key[1]) + } else { + storage_info = append(storage_info, param) + } + } + if len(storage_info) > 0 { + storage = fmt.Sprintf("%s,%s", storage, strings.Join(storage_info, ",")) + } + params["tpmstate0"] = storage + } +} + // 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..45848a84 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-0.raw,size=4M,version=v2.0"}, + output: &ConfigQemu{TPMState: map[string]interface{}{ + "file": "vm-1000-disk-0.raw", + "size": "4M", + "storage": "local-lvm", + "version": "v2.0", + "volume": "local-lvm:vm-1000-disk-0.raw", + }}, + }, // Node {name: "Node vmr nil", output: &ConfigQemu{}, From 35da75421ac7c665401a7338216eb5f65d09c6fe Mon Sep 17 00:00:00 2001 From: Martin Pywell Date: Wed, 13 Mar 2024 22:53:49 +1100 Subject: [PATCH 2/3] TPM cleanup --- proxmox/config_qemu.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index 377f6afe..b4840138 100644 --- a/proxmox/config_qemu.go +++ b/proxmox/config_qemu.go @@ -183,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) @@ -220,9 +223,6 @@ func (config ConfigQemu) CreateVm(vmr *VmRef, client *Client) (err error) { return fmt.Errorf("[ERROR] %q", err) } - // Create TPM State disk - config.CreateQemuTpmParams(params) - return } @@ -432,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) @@ -452,9 +455,6 @@ func (config ConfigQemu) mapToApiValues(currentConfig ConfigQemu) (rebootRequire config.CreateQemuPCIsParams(params) - // Create TPM state disk - config.CreateQemuTpmParams(params) - err = config.CreateIpconfigParams(params) if err != nil { log.Printf("[ERROR] %q", err) @@ -1758,21 +1758,21 @@ func (c ConfigQemu) CreateQemuTpmParams(params map[string]interface{}) { tpmParam = tpmParam.createDeviceParam(c.TPMState, nil) if len(tpmParam) > 0 { - storage_info := []string{} - storage := "" + tpm_info := []string{} + tpm := "" for _, param := range tpmParam { key := strings.Split(param, "=") if key[0] == "storage" { // Proxmox format for disk creation - storage = fmt.Sprintf("%s:1", key[1]) + tpm = fmt.Sprintf("%s:1", key[1]) } else { - storage_info = append(storage_info, param) + tpm_info = append(tpm_info, param) } } - if len(storage_info) > 0 { - storage = fmt.Sprintf("%s,%s", storage, strings.Join(storage_info, ",")) + if len(tpm_info) > 0 { + tpm = fmt.Sprintf("%s,%s", tpm, strings.Join(tpm_info, ",")) } - params["tpmstate0"] = storage + params["tpmstate0"] = tpm } } From 73f360a4f7d3476864b7150ecde1c8e0dbaccdff Mon Sep 17 00:00:00 2001 From: Martin Pywell Date: Wed, 13 Mar 2024 22:59:35 +1100 Subject: [PATCH 3/3] cleanup TPM test --- proxmox/config_qemu_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/proxmox/config_qemu_test.go b/proxmox/config_qemu_test.go index 45848a84..8b617f58 100644 --- a/proxmox/config_qemu_test.go +++ b/proxmox/config_qemu_test.go @@ -5765,13 +5765,13 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { }, // TPM {name: "TPM State", - input: map[string]interface{}{"tpmstate0": "local-lvm:vm-1000-disk-0.raw,size=4M,version=v2.0"}, + 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-0.raw", + "file": "vm-1000-disk-1.raw", "size": "4M", "storage": "local-lvm", "version": "v2.0", - "volume": "local-lvm:vm-1000-disk-0.raw", + "volume": "local-lvm:vm-1000-disk-1.raw", }}, }, // Node