Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: QEMU TPM State device support #321

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions proxmox/config_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -289,6 +293,9 @@ func (config *ConfigQemu) defaults() {
if config.Tablet == nil {
config.Tablet = util.Pointer(true)
}
if config.TPMState == nil {
config.TPMState = QemuDevice{}
}

}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions proxmox/config_qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
Loading