# VMID and IP Allocation

This page explains how go-pve-qemu picks a free VMID when an install request omits `id`, and how that VMID decides the VM's IP.

## VMID to IP Mapping

```text
GATEWAY = 192.168.0.1
VMID    = 120
IP      = 192.168.0.120/24   (first three octets of GATEWAY + VMID)
```

Because the last IP octet is the VMID, VMIDs must fall within `100`–`254`: below 100 is reserved by Proxmox, above 254 is not a valid host address.

## Probe Range

| Variable | Default | Correction |
|---|---|---|
| `ASSIGN_IP_START` | `100` | `0` or < 100 → 100 |
| `ASSIGN_IP_END` | `254` | `0` or > 254 → 254 |

A reversed range is swapped automatically.

## Two-Ended Concurrent Probe

Each round spawns one goroutine for `start` and one for `end`, then moves `start++` and `end--` until the two meet. A semaphore of size 10 caps concurrent probes.

Each candidate VMID is checked in order and dropped on the first hit:

| Order | Check | A hit means |
|---|---|---|
| 1 | `/etc/pve/nodes/*/qemu-server/<vmid>.conf` exists | Some node already has this VMID |
| 2 | `qm config <vmid>` succeeds | Proxmox knows this VMID |
| 3 | `192.168.0.<vmid>:22` accepts a connection within 2 seconds | A host already uses that IP |

The first VMID that passes all three is sent to the result channel, and every unfinished probe is cancelled.

## Results and Limits

| Case | Result |
|---|---|
| Free VMID found | Used as the VMID and last IP octet |
| Every VMID taken | `cannot find available IP` |
| No result within 10 seconds | `timeout: no available IP found` |

- Because both ends are probed at once, the result is not guaranteed to be the lowest free number
- Check 3 uses a fixed `192.168.0.` prefix in the current code and does not follow `GATEWAY`; on a different subnet it probes the wrong host
- An explicit `id` in the request skips every check
