Skip to content

Commit

Permalink
Merge pull request #263 from mabeett/add_rng_device
Browse files Browse the repository at this point in the history
feat: support VM VirtIO-based Random Number Generator configuration
  • Loading branch information
mleone87 authored May 24, 2023
2 parents 68d4b67 + 4f314bf commit 41e6ffa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ createQemu JSON Sample:
"tag": -1
}
},
"rng0": {
"source": "/dev/urandom",
"max_bytes": "1024",
"period": "1000"
},
"usb": {
"0": {
"host": "0658:0200",
Expand Down
31 changes: 31 additions & 0 deletions proxmox/config_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ConfigQemu struct {
Description string `json:"description,omitempty"`
Disks *QemuStorages `json:"disks,omitempty"`
EFIDisk QemuDevice `json:"efidisk,omitempty"` // TODO should be a struct
RNGDrive QemuDevice `json:"rng0,omitempty"` // TODO should be a struct
FullClone *int `json:"fullclone,omitempty"` // TODO should probably be a bool
HaGroup string `json:"hagroup,omitempty"`
HaState string `json:"hastate,omitempty"` // TODO should be custom type with enum
Expand Down Expand Up @@ -179,6 +180,9 @@ func (config ConfigQemu) CreateVm(vmr *VmRef, client *Client) (err error) {
// Create EFI disk
config.CreateQemuEfiParams(params)

// Create VirtIO RNG
config.CreateQemuRngParams(params)

// Create vga config.
vgaParam := QemuDeviceParam{}
vgaParam = vgaParam.createDeviceParam(config.QemuVga, nil)
Expand Down Expand Up @@ -226,6 +230,9 @@ func (config *ConfigQemu) defaults() {
if config.Bios == "" {
config.Bios = "seabios"
}
if config.RNGDrive == nil {
config.RNGDrive = QemuDevice{}
}
if config.EFIDisk == nil {
config.EFIDisk = QemuDevice{}
}
Expand Down Expand Up @@ -419,6 +426,9 @@ func (config ConfigQemu) mapToApiValues(currentConfig ConfigQemu) (rebootRequire
// Create EFI disk
config.CreateQemuEfiParams(params)

// Create VirtIO RNG
config.CreateQemuRngParams(params)

// Create networks config.
config.CreateQemuNetworksParams(params)

Expand Down Expand Up @@ -1503,6 +1513,25 @@ func (c ConfigQemu) CreateIpconfigParams(params map[string]interface{}) error {
return nil
}

// Create RNG parameter.
func (c ConfigQemu) CreateQemuRngParams(params map[string]interface{}) {
rngParam := QemuDeviceParam{}
rngParam = rngParam.createDeviceParam(c.RNGDrive, nil)

if len(rngParam) > 0 {
rng_info := []string{}
rng := ""
for _, param := range rngParam {
key := strings.Split(param, "=")
rng_info = append(rng_info, fmt.Sprintf("%s=%s", key[0], key[1]))
}
if len(rng_info) > 0 {
rng = strings.Join(rng_info, ",")
params["rng0"] = rng
}
}
}

// Create efi parameter.
func (c ConfigQemu) CreateQemuEfiParams(params map[string]interface{}) {
efiParam := QemuDeviceParam{}
Expand Down Expand Up @@ -1614,6 +1643,8 @@ func (p QemuDeviceParam) createDeviceParam(
confValue = sValue
} else if iValue, ok := value.(int); ok && iValue > 0 {
confValue = iValue
} else if iValue, ok := value.(float64); ok && iValue > 0 {
confValue = iValue
}
if confValue != nil {
deviceConf := fmt.Sprintf("%v=%v", key, confValue)
Expand Down

0 comments on commit 41e6ffa

Please sign in to comment.