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

[Issues 285, 289] Fix regressions for Tags , Pool scoped VM creation #286

Merged
merged 3 commits into from
Oct 28, 2024
Merged
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
8 changes: 4 additions & 4 deletions .web-docs/components/builder/clone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,23 @@ boot time.
- `boot` (string) - Override default boot order. Format example `order=virtio0;ide2;net0`.
Prior to Proxmox 6.2-15 the format was `cdn` (c:CDROM -> d:Disk -> n:Network)

- `memory` (int) - How much memory (in megabytes) to give the virtual
- `memory` (uint32) - How much memory (in megabytes) to give the virtual
machine. If `ballooning_minimum` is also set, `memory` defines the maximum amount
of memory the VM will be able to use.
Defaults to `512`.

- `ballooning_minimum` (int) - Setting this option enables KVM memory ballooning and
- `ballooning_minimum` (uint32) - Setting this option enables KVM memory ballooning and
defines the minimum amount of memory (in megabytes) the VM will have.
Defaults to `0` (memory ballooning disabled).

- `cores` (int) - How many CPU cores to give the virtual machine. Defaults
- `cores` (uint8) - How many CPU cores to give the virtual machine. Defaults
to `1`.

- `cpu_type` (string) - The CPU type to emulate. See the Proxmox API
documentation for the complete list of accepted values. For best
performance, set this to `host`. Defaults to `kvm64`.

- `sockets` (int) - How many CPU sockets to give the virtual machine.
- `sockets` (uint8) - How many CPU sockets to give the virtual machine.
Defaults to `1`

- `numa` (bool) - If true, support for non-uniform memory access (NUMA)
Expand Down
8 changes: 4 additions & 4 deletions .web-docs/components/builder/iso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ in the image's Cloud-Init settings for provisioning.
- `boot` (string) - Override default boot order. Format example `order=virtio0;ide2;net0`.
Prior to Proxmox 6.2-15 the format was `cdn` (c:CDROM -> d:Disk -> n:Network)

- `memory` (int) - How much memory (in megabytes) to give the virtual
- `memory` (uint32) - How much memory (in megabytes) to give the virtual
machine. If `ballooning_minimum` is also set, `memory` defines the maximum amount
of memory the VM will be able to use.
Defaults to `512`.

- `ballooning_minimum` (int) - Setting this option enables KVM memory ballooning and
- `ballooning_minimum` (uint32) - Setting this option enables KVM memory ballooning and
defines the minimum amount of memory (in megabytes) the VM will have.
Defaults to `0` (memory ballooning disabled).

- `cores` (int) - How many CPU cores to give the virtual machine. Defaults
- `cores` (uint8) - How many CPU cores to give the virtual machine. Defaults
to `1`.

- `cpu_type` (string) - The CPU type to emulate. See the Proxmox API
documentation for the complete list of accepted values. For best
performance, set this to `host`. Defaults to `kvm64`.

- `sockets` (int) - How many CPU sockets to give the virtual machine.
- `sockets` (uint8) - How many CPU sockets to give the virtual machine.
Defaults to `1`

- `numa` (bool) - If true, support for non-uniform memory access (NUMA)
Expand Down
79 changes: 72 additions & 7 deletions builder/proxmox/clone/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
package proxmoxclone

import (
"crypto"
"net/netip"
"strings"

proxmoxapi "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/hcl/v2/hcldec"
proxmox "github.com/hashicorp/packer-plugin-proxmox/builder/proxmox/common"
Expand Down Expand Up @@ -61,17 +65,78 @@ func (*cloneVMCreator) Create(vmRef *proxmoxapi.VmRef, config proxmoxapi.ConfigQ
config.FullClone = &fullClone

// cloud-init options
config.CIuser = comm.SSHUsername
config.Sshkeys = string(comm.SSHPublicKey)
config.Nameserver = c.Nameserver
config.Searchdomain = c.Searchdomain
IpconfigMap := make(map[int]interface{})

var nameServers []netip.Addr
if c.Nameserver != "" {
for _, nameserver := range strings.Split(c.Nameserver, " ") {
ip, _ := netip.ParseAddr(nameserver)
nameServers = append(nameServers, ip)
}
}

IpconfigMap := proxmoxapi.CloudInitNetworkInterfaces{}
for idx := range c.Ipconfigs {
if c.Ipconfigs[idx] != (cloudInitIpconfig{}) {
IpconfigMap[idx] = c.Ipconfigs[idx].String()

// backwards compatibility conversions

var ipv4cfg proxmoxapi.CloudInitIPv4Config
var ipv6cfg proxmoxapi.CloudInitIPv6Config

// cloudInitIpconfig.Ip accepts a CIDR address or 'dhcp' string
switch c.Ipconfigs[idx].Ip {
case "dhcp":
ipv4cfg.DHCP = true
default:
if c.Ipconfigs[idx].Ip != "" {
addr := proxmoxapi.IPv4CIDR(c.Ipconfigs[idx].Ip)
ipv4cfg.Address = &addr
}
}
if c.Ipconfigs[idx].Gateway != "" {
gw := proxmoxapi.IPv4Address(c.Ipconfigs[idx].Gateway)
ipv4cfg.Gateway = &gw
}

// cloudInitIpconfig.Ip6 accepts a CIDR address, 'auto' or 'dhcp' string
switch c.Ipconfigs[idx].Ip6 {
case "dhcp":
ipv6cfg.DHCP = true
case "auto":
ipv6cfg.SLAAC = true
default:
if c.Ipconfigs[idx].Ip6 != "" {
addr := proxmoxapi.IPv6CIDR(c.Ipconfigs[idx].Ip6)
ipv6cfg.Address = &addr
}
}
if c.Ipconfigs[idx].Gateway6 != "" {
addr := proxmoxapi.IPv6Address(c.Ipconfigs[idx].Gateway6)
ipv6cfg.Gateway = &addr
}

IpconfigMap[proxmoxapi.QemuNetworkInterfaceID(idx)] = proxmoxapi.CloudInitNetworkConfig{
IPv4: &ipv4cfg,
IPv6: &ipv6cfg,
}
}
}
config.Ipconfig = IpconfigMap

var publicKey []crypto.PublicKey

if comm.SSHPublicKey != nil {
publicKey = append(publicKey, crypto.PublicKey(string(comm.SSHPublicKey)))
}

config.CloudInit = &proxmoxapi.CloudInit{
Username: &comm.SSHUsername,
PublicSSHkeys: &publicKey,
DNS: &proxmoxapi.GuestDNS{
NameServers: &nameServers,
SearchDomain: &c.Searchdomain,
},
NetworkInterfaces: IpconfigMap,
}

var sourceVmr *proxmoxapi.VmRef
if c.CloneVM != "" {
Expand Down
8 changes: 4 additions & 4 deletions builder/proxmox/clone/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions builder/proxmox/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,21 @@ type Config struct {
// machine. If `ballooning_minimum` is also set, `memory` defines the maximum amount
// of memory the VM will be able to use.
// Defaults to `512`.
Memory int `mapstructure:"memory"`
Memory uint32 `mapstructure:"memory"`
// Setting this option enables KVM memory ballooning and
// defines the minimum amount of memory (in megabytes) the VM will have.
// Defaults to `0` (memory ballooning disabled).
BalloonMinimum int `mapstructure:"ballooning_minimum"`
BalloonMinimum uint32 `mapstructure:"ballooning_minimum"`
// How many CPU cores to give the virtual machine. Defaults
// to `1`.
Cores int `mapstructure:"cores"`
Cores uint8 `mapstructure:"cores"`
// The CPU type to emulate. See the Proxmox API
// documentation for the complete list of accepted values. For best
// performance, set this to `host`. Defaults to `kvm64`.
CPUType string `mapstructure:"cpu_type"`
// How many CPU sockets to give the virtual machine.
// Defaults to `1`
Sockets int `mapstructure:"sockets"`
Sockets uint8 `mapstructure:"sockets"`
// If true, support for non-uniform memory access (NUMA)
// is enabled. Defaults to `false`.
Numa bool `mapstructure:"numa"`
Expand Down Expand Up @@ -678,6 +678,11 @@ func (c *Config) Prepare(upper interface{}, raws ...interface{}) ([]string, []st
// (possibly to a local file) to an ISO file that will be downloaded and
// then uploaded to Proxmox.
if c.ISOs[idx].ISOFile != "" {
// ISOFile should match <storage>:iso/<ISO filename> format
res := regexp.MustCompile(`^.+:iso\/.+$`)
if !res.MatchString(c.ISOs[idx].ISOFile) {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("iso_path should match pattern \"<storage>:iso/<ISO filename>\". Provided value was \"%s\"", c.ISOs[idx].ISOFile))
}
c.ISOs[idx].ShouldUploadISO = false
} else {
c.ISOs[idx].DownloadPathKey = "downloaded_additional_iso_path_" + strconv.Itoa(idx)
Expand Down
8 changes: 4 additions & 4 deletions builder/proxmox/common/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions builder/proxmox/common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ func TestISOs(t *testing.T) {
"type": "ide",
},
},
{
name: "iso_file should fail if not correct format",
expectedToFail: true,
ISOs: map[string]interface{}{
"type": "ide",
"cd_files": []string{
"config_test.go",
},
"iso_file": "local:/test.iso",
},
},
{
name: "cd_files and iso_file specified should fail",
expectedToFail: true,
Expand Down
49 changes: 31 additions & 18 deletions builder/proxmox/common/step_start_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,23 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
}
}

var description = "Packer ephemeral build VM"

config := proxmox.ConfigQemu{
Name: c.VMName,
Agent: generateAgentConfig(c.Agent),
QemuKVM: &kvm,
Tags: generateTags(c.Tags),
Boot: c.Boot, // Boot priority, example: "order=virtio0;ide2;net0", virtio0:Disk0 -> ide0:CDROM -> net0:Network
QemuCpu: c.CPUType,
Description: "Packer ephemeral build VM",
Memory: c.Memory,
QemuCores: c.Cores,
QemuSockets: c.Sockets,
QemuNuma: &c.Numa,
Name: c.VMName,
Agent: generateAgentConfig(c.Agent),
QemuKVM: &kvm,
Tags: generateTags(c.Tags),
Boot: c.Boot, // Boot priority, example: "order=virtio0;ide2;net0", virtio0:Disk0 -> ide0:CDROM -> net0:Network
CPU: &proxmox.QemuCPU{
Cores: (*proxmox.QemuCpuCores)(&c.Cores),
Sockets: (*proxmox.QemuCpuSockets)(&c.Sockets),
Numa: &c.Numa,
},
Description: &description,
Memory: &proxmox.QemuMemory{
CapacityMiB: (*proxmox.QemuMemoryCapacity)(&c.Memory),
},
QemuOs: c.OS,
Bios: c.BIOS,
EFIDisk: generateProxmoxEfi(c.EFIConfig),
Expand All @@ -141,7 +146,7 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
QemuNetworks: generateProxmoxNetworkAdapters(c.NICs),
Disks: disks,
QemuPCIDevices: generateProxmoxPCIDeviceMap(c.PCIDevices),
QemuSerials: generateProxmoxSerials(c.Serials),
Serials: generateProxmoxSerials(c.Serials),
Scsihw: c.SCSIController,
Onboot: &c.Onboot,
Args: c.AdditionalArgs,
Expand All @@ -152,7 +157,7 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
// and should be kept enabled by default.
// See https://github.com/hashicorp/packer-plugin-proxmox/issues/127#issuecomment-1464030102
if c.BalloonMinimum > 0 {
config.Balloon = c.BalloonMinimum
config.Memory.MinimumCapacityMiB = (*proxmox.QemuMemoryBalloonCapacity)(&c.BalloonMinimum)
}

if c.PackerForce {
Expand Down Expand Up @@ -695,11 +700,19 @@ func generateProxmoxPCIDeviceMap(devices []pciDeviceConfig) proxmox.QemuDevices
return devs
}

func generateProxmoxSerials(serials []string) proxmox.QemuDevices {
devs := make(proxmox.QemuDevices)
for idx := range serials {
devs[idx] = make(proxmox.QemuDevice)
setDeviceParamIfDefined(devs[idx], "type", serials[idx])
func generateProxmoxSerials(serials []string) proxmox.SerialInterfaces {
devs := make(proxmox.SerialInterfaces)
for idx, serial := range serials {
switch serial {
case "socket":
devs[proxmox.SerialID(idx)] = proxmox.SerialInterface{
Socket: true,
}
default:
devs[proxmox.SerialID(idx)] = proxmox.SerialInterface{
Path: proxmox.SerialPath(serial),
}
}
}
return devs
}
Expand Down
8 changes: 4 additions & 4 deletions builder/proxmox/iso/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@
- `boot` (string) - Override default boot order. Format example `order=virtio0;ide2;net0`.
Prior to Proxmox 6.2-15 the format was `cdn` (c:CDROM -> d:Disk -> n:Network)

- `memory` (int) - How much memory (in megabytes) to give the virtual
- `memory` (uint32) - How much memory (in megabytes) to give the virtual
machine. If `ballooning_minimum` is also set, `memory` defines the maximum amount
of memory the VM will be able to use.
Defaults to `512`.

- `ballooning_minimum` (int) - Setting this option enables KVM memory ballooning and
- `ballooning_minimum` (uint32) - Setting this option enables KVM memory ballooning and
defines the minimum amount of memory (in megabytes) the VM will have.
Defaults to `0` (memory ballooning disabled).

- `cores` (int) - How many CPU cores to give the virtual machine. Defaults
- `cores` (uint8) - How many CPU cores to give the virtual machine. Defaults
to `1`.

- `cpu_type` (string) - The CPU type to emulate. See the Proxmox API
documentation for the complete list of accepted values. For best
performance, set this to `host`. Defaults to `kvm64`.

- `sockets` (int) - How many CPU sockets to give the virtual machine.
- `sockets` (uint8) - How many CPU sockets to give the virtual machine.
Defaults to `1`

- `numa` (bool) - If true, support for non-uniform memory access (NUMA)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/hashicorp/packer-plugin-proxmox
go 1.21.0

require (
github.com/Telmate/proxmox-api-go v0.0.0-20240525163725-6676d8933df0
github.com/Telmate/proxmox-api-go v0.0.0-20241022204517-b149708f750b
github.com/hashicorp/go-getter/v2 v2.2.2
github.com/hashicorp/hcl/v2 v2.19.1
github.com/hashicorp/packer-plugin-sdk v0.5.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/Telmate/proxmox-api-go v0.0.0-20240409105641-32c480fe008e h1:NdpVflh7
github.com/Telmate/proxmox-api-go v0.0.0-20240409105641-32c480fe008e/go.mod h1:bscBzOUx0tJAdVGmQvcnoWPg5eI2eJ6anJKV1ueZ1oU=
github.com/Telmate/proxmox-api-go v0.0.0-20240525163725-6676d8933df0 h1:VK35Q0s1IayA2Nwm0uREu+LVs0WWPq9Zsn3Oic1q5eg=
github.com/Telmate/proxmox-api-go v0.0.0-20240525163725-6676d8933df0/go.mod h1:bscBzOUx0tJAdVGmQvcnoWPg5eI2eJ6anJKV1ueZ1oU=
github.com/Telmate/proxmox-api-go v0.0.0-20241022204517-b149708f750b h1:vgK9CbUZpd37RYab6lCEQTyP0fw/Ygq5NOWctBI5rpA=
github.com/Telmate/proxmox-api-go v0.0.0-20241022204517-b149708f750b/go.mod h1:Gu6n6vEn1hlyFUkjrvU+X1fdgaSXLoM9HKYYJqy1fsY=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down