Skip to content

Commit

Permalink
Check if vm_id is in valid range.
Browse files Browse the repository at this point in the history
Technically Proxmox VMIDs are unsigned 32bit integers, but are limited to
the range 100-999999999. Source:
https://pve-devel.pve.proxmox.narkive.com/Pa6mH1OP/avoiding-vmid-reuse#post8
Check if vm_id is in that range.
  • Loading branch information
sebastian-de authored and lbajolet-hashicorp committed Feb 24, 2023
1 parent 4842a84 commit 9cb889b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
6 changes: 6 additions & 0 deletions builder/proxmox/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func (c *Config) Prepare(upper interface{}, raws ...interface{}) ([]string, []st
c.BootKeyInterval = 5 * time.Millisecond
}

// Technically Proxmox VMIDs are unsigned 32bit integers, but are limited to
// the range 100-999999999. Source:
// https://pve-devel.pve.proxmox.narkive.com/Pa6mH1OP/avoiding-vmid-reuse#post8
if c.VMID != 0 && (c.VMID < 100 || c.VMID > 999999999) {
errs = packersdk.MultiErrorAppend(errs, errors.New("vm_id must be in range 100-999999999"))
}
if c.VMName == "" {
// Default to packer-[time-ordered-uuid]
c.VMName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
Expand Down
49 changes: 49 additions & 0 deletions builder/proxmox/common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,52 @@ func TestSerials(t *testing.T) {
})
}
}

func TestVMID(t *testing.T) {
serialsTest := []struct {
name string
VMID int
expectFailure bool
}{
{
name: "VMID zero, no error",
expectFailure: false,
VMID: 0,
},
{
name: "VMID in range, no error",
expectFailure: false,
VMID: 1000,
},
{
name: "VMID above range, fail",
expectFailure: true,
VMID: 1000000000,
},
{
name: "VMID below range, fail",
expectFailure: true,
VMID: 50,
},
}

for _, tt := range serialsTest {
t.Run(tt.name, func(t *testing.T) {
cfg := mandatoryConfig(t)
cfg["vm_id"] = tt.VMID

var c Config
_, _, err := c.Prepare(&c, cfg)
if err != nil {
if !tt.expectFailure {
t.Fatalf("unexpected failure to prepare config: %s", err)
}
t.Logf("got expected failure: %s", err)
}

if err == nil && tt.expectFailure {
t.Errorf("expected failure, but prepare succeeded")
}
})
}
}
5 changes: 3 additions & 2 deletions docs/builders/clone.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ in the image's Cloud-Init settings for provisioning.
given, a random uuid will be used.

- `vm_id` (int) - The ID used to reference the virtual machine. This will
also be the ID of the final template. If not given, the next free ID on
the node will be used.
also be the ID of the final template. Proxmox VMIDs are unique cluster-wide
and are limited to the range 100-999999999.
If not given, the next free ID on the cluster will be used.

- `memory` (int) - How much memory, in megabytes, to give the virtual
machine. Defaults to `512`.
Expand Down
5 changes: 3 additions & 2 deletions docs/builders/iso.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ in the image's Cloud-Init settings for provisioning.
given, a random uuid will be used.

- `vm_id` (int) - The ID used to reference the virtual machine. This will
also be the ID of the final template. If not given, the next free ID on
the node will be used.
also be the ID of the final template. Proxmox VMIDs are unique cluster-wide
and are limited to the range 100-999999999.
If not given, the next free ID on the cluster will be used.

- `memory` (int) - How much memory, in megabytes, to give the virtual
machine. Defaults to `512`.
Expand Down

0 comments on commit 9cb889b

Please sign in to comment.