From b4827ee3e47b0123d655785a9ef7d4bee436de29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Pecchia?= <179218+mabeett@users.noreply.github.com> Date: Fri, 26 May 2023 23:07:37 +0200 Subject: [PATCH] add rng device: to be squashed --- builder/proxmox/clone/config.hcl2spec.go | 2 ++ builder/proxmox/common/config.go | 22 +++++++++++++- builder/proxmox/common/config.hcl2spec.go | 29 +++++++++++++++++++ builder/proxmox/common/config_test.go | 2 ++ builder/proxmox/common/step_start_vm.go | 14 +++++++++ builder/proxmox/iso/config.hcl2spec.go | 2 ++ .../proxmox/common/Config-not-required.mdx | 2 ++ .../common/rng0Config-not-required.mdx | 9 ++++++ docs/builders/clone.mdx | 17 +++++++++++ docs/builders/iso.mdx | 2 ++ 10 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 docs-partials/builder/proxmox/common/rng0Config-not-required.mdx diff --git a/builder/proxmox/clone/config.hcl2spec.go b/builder/proxmox/clone/config.hcl2spec.go index 781e4a65..10fe821a 100644 --- a/builder/proxmox/clone/config.hcl2spec.go +++ b/builder/proxmox/clone/config.hcl2spec.go @@ -99,6 +99,7 @@ type FlatConfig struct { EFIConfig *proxmox.FlatefiConfig `mapstructure:"efi_config" cty:"efi_config" hcl:"efi_config"` EFIDisk *string `mapstructure:"efidisk" cty:"efidisk" hcl:"efidisk"` Machine *string `mapstructure:"machine" cty:"machine" hcl:"machine"` + Rng0 *proxmox.Flatrng0Config `mapstructure:"rng0" cty:"rng0" hcl:"rng0"` VGA *proxmox.FlatvgaConfig `mapstructure:"vga" cty:"vga" hcl:"vga"` NICs []proxmox.FlatNICConfig `mapstructure:"network_adapters" cty:"network_adapters" hcl:"network_adapters"` Disks []proxmox.FlatdiskConfig `mapstructure:"disks" cty:"disks" hcl:"disks"` @@ -221,6 +222,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "efi_config": &hcldec.BlockSpec{TypeName: "efi_config", Nested: hcldec.ObjectSpec((*proxmox.FlatefiConfig)(nil).HCL2Spec())}, "efidisk": &hcldec.AttrSpec{Name: "efidisk", Type: cty.String, Required: false}, "machine": &hcldec.AttrSpec{Name: "machine", Type: cty.String, Required: false}, + "rng0": &hcldec.BlockSpec{TypeName: "rng0", Nested: hcldec.ObjectSpec((*proxmox.Flatrng0Config)(nil).HCL2Spec())}, "vga": &hcldec.BlockSpec{TypeName: "vga", Nested: hcldec.ObjectSpec((*proxmox.FlatvgaConfig)(nil).HCL2Spec())}, "network_adapters": &hcldec.BlockListSpec{TypeName: "network_adapters", Nested: hcldec.ObjectSpec((*proxmox.FlatNICConfig)(nil).HCL2Spec())}, "disks": &hcldec.BlockListSpec{TypeName: "disks", Nested: hcldec.ObjectSpec((*proxmox.FlatdiskConfig)(nil).HCL2Spec())}, diff --git a/builder/proxmox/common/config.go b/builder/proxmox/common/config.go index c22fcca1..75afcaeb 100644 --- a/builder/proxmox/common/config.go +++ b/builder/proxmox/common/config.go @@ -2,7 +2,7 @@ // SPDX-License-Identifier: MPL-2.0 //go:generate packer-sdc struct-markdown -//go:generate packer-sdc mapstructure-to-hcl2 -type Config,NICConfig,diskConfig,vgaConfig,additionalISOsConfig,efiConfig +//go:generate packer-sdc mapstructure-to-hcl2 -type Config,NICConfig,diskConfig,rng0Config,vgaConfig,additionalISOsConfig,efiConfig package proxmox @@ -59,6 +59,7 @@ type Config struct { EFIConfig efiConfig `mapstructure:"efi_config"` EFIDisk string `mapstructure:"efidisk"` Machine string `mapstructure:"machine"` + Rng0 rng0Config `mapstructure:"rng0"` VGA vgaConfig `mapstructure:"vga"` NICs []NICConfig `mapstructure:"network_adapters"` Disks []diskConfig `mapstructure:"disks"` @@ -114,6 +115,13 @@ type efiConfig struct { PreEnrolledKeys bool `mapstructure:"pre_enrolled_keys"` EFIType string `mapstructure:"efi_type"` } + +type rng0Config struct { + Source string `mapstructure:"source"` + MaxBytes int `mapstructure:"max_bytes"` + Period int `mapstructure:"period"` +} + type vgaConfig struct { Type string `mapstructure:"type"` Memory int `mapstructure:"memory"` @@ -391,6 +399,18 @@ func (c *Config) Prepare(upper interface{}, raws ...interface{}) ([]string, []st errs = packersdk.MultiErrorAppend(errs, errors.New("efi_storage_pool not set for efi_config")) } } + if c.Rng0.Source != "" { + if !(c.Rng0.Source == "/dev/urandom" || c.Rng0.Source == "/dev/random" || c.Rng0.Source == "/dev/hwrng") { + errs = packersdk.MultiErrorAppend(errs, errors.New("source must be one of \"/dev/urandom\", \"/dev/random\", \"/dev/hwrng\"")) + } + // FIXME might be not declared by the user then not send to proxmox PVE API + if c.Rng0.MaxBytes < 0 { + errs = packersdk.MultiErrorAppend(errs, errors.New("max_bytes must be greather or equal than 0")) + } + if !(c.Rng0.Period > 0) { + errs = packersdk.MultiErrorAppend(errs, errors.New("period must be greather than 0")) + } + } if errs != nil && len(errs.Errors) > 0 { return nil, warnings, errs diff --git a/builder/proxmox/common/config.hcl2spec.go b/builder/proxmox/common/config.hcl2spec.go index 829029a1..0b8a5ba7 100644 --- a/builder/proxmox/common/config.hcl2spec.go +++ b/builder/proxmox/common/config.hcl2spec.go @@ -98,6 +98,7 @@ type FlatConfig struct { EFIConfig *FlatefiConfig `mapstructure:"efi_config" cty:"efi_config" hcl:"efi_config"` EFIDisk *string `mapstructure:"efidisk" cty:"efidisk" hcl:"efidisk"` Machine *string `mapstructure:"machine" cty:"machine" hcl:"machine"` + Rng0 *Flatrng0Config `mapstructure:"rng0" cty:"rng0" hcl:"rng0"` VGA *FlatvgaConfig `mapstructure:"vga" cty:"vga" hcl:"vga"` NICs []FlatNICConfig `mapstructure:"network_adapters" cty:"network_adapters" hcl:"network_adapters"` Disks []FlatdiskConfig `mapstructure:"disks" cty:"disks" hcl:"disks"` @@ -214,6 +215,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "efi_config": &hcldec.BlockSpec{TypeName: "efi_config", Nested: hcldec.ObjectSpec((*FlatefiConfig)(nil).HCL2Spec())}, "efidisk": &hcldec.AttrSpec{Name: "efidisk", Type: cty.String, Required: false}, "machine": &hcldec.AttrSpec{Name: "machine", Type: cty.String, Required: false}, + "rng0": &hcldec.BlockSpec{TypeName: "rng0", Nested: hcldec.ObjectSpec((*Flatrng0Config)(nil).HCL2Spec())}, "vga": &hcldec.BlockSpec{TypeName: "vga", Nested: hcldec.ObjectSpec((*FlatvgaConfig)(nil).HCL2Spec())}, "network_adapters": &hcldec.BlockListSpec{TypeName: "network_adapters", Nested: hcldec.ObjectSpec((*FlatNICConfig)(nil).HCL2Spec())}, "disks": &hcldec.BlockListSpec{TypeName: "disks", Nested: hcldec.ObjectSpec((*FlatdiskConfig)(nil).HCL2Spec())}, @@ -374,6 +376,33 @@ func (*FlatefiConfig) HCL2Spec() map[string]hcldec.Spec { return s } +// Flatrng0Config is an auto-generated flat version of rng0Config. +// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. +type Flatrng0Config struct { + Source *string `mapstructure:"source" cty:"source" hcl:"source"` + MaxBytes *int `mapstructure:"max_bytes" cty:"max_bytes" hcl:"max_bytes"` + Period *int `mapstructure:"period" cty:"period" hcl:"period"` +} + +// FlatMapstructure returns a new Flatrng0Config. +// Flatrng0Config is an auto-generated flat version of rng0Config. +// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up. +func (*rng0Config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { + return new(Flatrng0Config) +} + +// HCL2Spec returns the hcl spec of a rng0Config. +// This spec is used by HCL to read the fields of rng0Config. +// The decoded values from this spec will then be applied to a Flatrng0Config. +func (*Flatrng0Config) HCL2Spec() map[string]hcldec.Spec { + s := map[string]hcldec.Spec{ + "source": &hcldec.AttrSpec{Name: "source", Type: cty.String, Required: false}, + "max_bytes": &hcldec.AttrSpec{Name: "max_bytes", Type: cty.Number, Required: false}, + "period": &hcldec.AttrSpec{Name: "period", Type: cty.Number, Required: false}, + } + return s +} + // FlatvgaConfig is an auto-generated flat version of vgaConfig. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. type FlatvgaConfig struct { diff --git a/builder/proxmox/common/config_test.go b/builder/proxmox/common/config_test.go index 855db967..0bb3b537 100644 --- a/builder/proxmox/common/config_test.go +++ b/builder/proxmox/common/config_test.go @@ -246,6 +246,8 @@ func TestAdditionalISOs(t *testing.T) { } +// FIXME Tests for Rng0 + func TestSerials(t *testing.T) { serialsTest := []struct { name string diff --git a/builder/proxmox/common/step_start_vm.go b/builder/proxmox/common/step_start_vm.go index f45faf99..4b40061f 100644 --- a/builder/proxmox/common/step_start_vm.go +++ b/builder/proxmox/common/step_start_vm.go @@ -123,6 +123,7 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist Bios: c.BIOS, EFIDisk: generateProxmoxEfi(c.EFIConfig), Machine: c.Machine, + RNGDrive: generateProxmoxRng0(c.Rng0), QemuVga: generateProxmoxVga(c.VGA), QemuNetworks: generateProxmoxNetworkAdapters(c.NICs), QemuDisks: generateProxmoxDisks(c.Disks), @@ -295,6 +296,19 @@ func generateProxmoxSerials(serials []string) proxmox.QemuDevices { return devs } +func generateProxmoxRng0(rng0 rng0Config) proxmox.QemuDevice { + dev := make(proxmox.QemuDevice) + setDeviceParamIfDefined(dev, "source", rng0.Source) + + if rng0.MaxBytes > 0 { + dev["max_bytes"] = rng0.MaxBytes + } + if rng0.Period > 0 { + dev["period"] = rng0.Period + } + return dev +} + func generateProxmoxVga(vga vgaConfig) proxmox.QemuDevice { dev := make(proxmox.QemuDevice) setDeviceParamIfDefined(dev, "type", vga.Type) diff --git a/builder/proxmox/iso/config.hcl2spec.go b/builder/proxmox/iso/config.hcl2spec.go index d0ed202d..c64c54ed 100644 --- a/builder/proxmox/iso/config.hcl2spec.go +++ b/builder/proxmox/iso/config.hcl2spec.go @@ -99,6 +99,7 @@ type FlatConfig struct { EFIConfig *proxmox.FlatefiConfig `mapstructure:"efi_config" cty:"efi_config" hcl:"efi_config"` EFIDisk *string `mapstructure:"efidisk" cty:"efidisk" hcl:"efidisk"` Machine *string `mapstructure:"machine" cty:"machine" hcl:"machine"` + Rng0 *proxmox.Flatrng0Config `mapstructure:"rng0" cty:"rng0" hcl:"rng0"` VGA *proxmox.FlatvgaConfig `mapstructure:"vga" cty:"vga" hcl:"vga"` NICs []proxmox.FlatNICConfig `mapstructure:"network_adapters" cty:"network_adapters" hcl:"network_adapters"` Disks []proxmox.FlatdiskConfig `mapstructure:"disks" cty:"disks" hcl:"disks"` @@ -224,6 +225,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "efi_config": &hcldec.BlockSpec{TypeName: "efi_config", Nested: hcldec.ObjectSpec((*proxmox.FlatefiConfig)(nil).HCL2Spec())}, "efidisk": &hcldec.AttrSpec{Name: "efidisk", Type: cty.String, Required: false}, "machine": &hcldec.AttrSpec{Name: "machine", Type: cty.String, Required: false}, + "rng0": &hcldec.BlockSpec{TypeName: "rng0", Nested: hcldec.ObjectSpec((*proxmox.Flatrng0Config)(nil).HCL2Spec())}, "vga": &hcldec.BlockSpec{TypeName: "vga", Nested: hcldec.ObjectSpec((*proxmox.FlatvgaConfig)(nil).HCL2Spec())}, "network_adapters": &hcldec.BlockListSpec{TypeName: "network_adapters", Nested: hcldec.ObjectSpec((*proxmox.FlatNICConfig)(nil).HCL2Spec())}, "disks": &hcldec.BlockListSpec{TypeName: "disks", Nested: hcldec.ObjectSpec((*proxmox.FlatdiskConfig)(nil).HCL2Spec())}, diff --git a/docs-partials/builder/proxmox/common/Config-not-required.mdx b/docs-partials/builder/proxmox/common/Config-not-required.mdx index 60e21c8d..7d43201d 100644 --- a/docs-partials/builder/proxmox/common/Config-not-required.mdx +++ b/docs-partials/builder/proxmox/common/Config-not-required.mdx @@ -44,6 +44,8 @@ - `machine` (string) - Machine +- `rng0` (rng0Config) - Rng 0 + - `vga` (vgaConfig) - VGA - `network_adapters` ([]NICConfig) - NI Cs diff --git a/docs-partials/builder/proxmox/common/rng0Config-not-required.mdx b/docs-partials/builder/proxmox/common/rng0Config-not-required.mdx new file mode 100644 index 00000000..d6c433cf --- /dev/null +++ b/docs-partials/builder/proxmox/common/rng0Config-not-required.mdx @@ -0,0 +1,9 @@ + + +- `source` (string) - Source + +- `max_bytes` (int) - Max Bytes + +- `period` (int) - Period + + diff --git a/docs/builders/clone.mdx b/docs/builders/clone.mdx index 69b65aec..f65f26ab 100644 --- a/docs/builders/clone.mdx +++ b/docs/builders/clone.mdx @@ -354,6 +354,23 @@ or responding to pattern `/dev/.+`. Example: - `machine` - (string) - Set the machine type. Supported values are 'pc' or 'q35'. +- `rng0` (object) - TODO + + ```json + { + "source": "xxx", + "max_bytes": xxx, + "period": xx + } + ``` + + - `source` (string) - TODO + + - `max_bytes` (int) - TODO + + - `period` (int) - TODO + + ## Example: Cloud-Init enabled Debian Here is a basic example creating a Debian 10 server image. This assumes diff --git a/docs/builders/iso.mdx b/docs/builders/iso.mdx index dece584f..bf774092 100644 --- a/docs/builders/iso.mdx +++ b/docs/builders/iso.mdx @@ -399,6 +399,8 @@ or responding to pattern `/dev/.+`. Example: - `machine` - (string) - Set the machine type. Supported values are 'pc' or 'q35'. +- `rng0` (object) - TODO + ## Boot Command @include 'packer-plugin-sdk/bootcommand/BootConfig.mdx'