diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index de8351f..e0b2950 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -54,13 +54,11 @@ class ProxmoxVMProvisionMixin: template: str, cpu: int, memory: int, - mgmt_bridge: str, - mgmt_vlan_tag: int | None, - capture_bridge: str, - capture_vlan_tags: List[int], + nics: List[Dict[str, Any]], cloud_init_config: Dict[str, Any], ssh_public_keys: List[str] | None = None, - timeout: int = 120, + disk_resize_gb: int | None = None, + timeout: int = 180, ) -> VMProvisionResultDict: """ Create a new VM from a Cloud-Init template via Proxmox API. @@ -68,24 +66,23 @@ class ProxmoxVMProvisionMixin: Steps: 1. Get next available VMID from cluster 2. Clone template VM (full clone, new VMID) - 3. Configure CPU, memory, and dual NICs (mgmt + capture) + 3. Configure CPU, memory, and network interfaces 4. Verify snippet storage exists 5. Render cloud-init config to YAML and upload 6. Set Cloud-Init config references and SSH keys - 7. Start the VM - 8. Return VMID, name, node + 7. Optionally resize root disk + 8. Start the VM + 9. Return VMID, name, node Args: name: new VM display name template: template VMID/name to clone from cpu: number of vCPUs memory: RAM in MB - mgmt_bridge: management bridge name - mgmt_vlan_tag: VLAN tag for mgmt NIC (None = untagged) - capture_bridge: packet capture bridge name (must be VLAN-aware) - capture_vlan_tags: list of VLAN IDs for capture NIC (trunk) + nics: list of NIC config dicts (bridge, vlan_tag/trunk_vlan_tags, dhcp flag) cloud_init_config: user-data dict (will be YAML-rendered) ssh_public_keys: SSH public keys to inject + disk_resize_gb: resize root disk to this size (None = no resize) timeout: max seconds for provisioning Returns: @@ -112,24 +109,30 @@ class ProxmoxVMProvisionMixin: ) self._wait_for_task(clone_upid, timeout=timeout) - # Step 3: Configure CPU, memory, and dual NICs - _logger.info(f"Configuring VM {vmid}: {cpu} CPU, {memory}MB RAM") + # Step 3: Configure CPU, memory, and NICs + _logger.info(f"Configuring VM {vmid}: {cpu} CPU, {memory}MB RAM, {len(nics)} NIC(s)") - # Build net0 (mgmt) config - net0_config = f"virtio,bridge={mgmt_bridge}" - if mgmt_vlan_tag is not None: - net0_config += f",tag={mgmt_vlan_tag}" + config_args = {"cores": cpu, "memory": memory} - # Build net1 (capture trunk) config - vlan_list = ";".join(str(v) for v in capture_vlan_tags) - net1_config = f"virtio,bridge={capture_bridge},trunks={vlan_list}" + # Build NIC config strings generically + for i, nic in enumerate(nics): + bridge = nic.get("bridge") + if not bridge: + raise ValueError(f"NIC {i}: bridge is required") - self._node_api().qemu(vmid).config.post( - cores=cpu, - memory=memory, - net0=net0_config, - net1=net1_config, - ) + # Build base config: model + bridge + net_config = f"virtio,bridge={bridge}" + + # Add VLAN configuration (access vs trunk) + if "trunk_vlan_tags" in nic and nic["trunk_vlan_tags"]: + vlan_list = ";".join(str(v) for v in nic["trunk_vlan_tags"]) + net_config += f",trunks={vlan_list}" + elif "vlan_tag" in nic and nic["vlan_tag"] is not None: + net_config += f",tag={nic['vlan_tag']}" + + config_args[f"net{i}"] = net_config + + self._node_api().qemu(vmid).config.post(**config_args) # Step 4: Verify snippet storage exists _logger.info("Checking for snippet storage...") @@ -152,12 +155,6 @@ class ProxmoxVMProvisionMixin: # Step 5: Render and upload Cloud-Init config _logger.info(f"Rendering Cloud-Init config for VMID {vmid}") - # Ensure cloud_init_config includes runcmd to bring up capture NIC - if "runcmd" not in cloud_init_config: - cloud_init_config["runcmd"] = [] - if not any("eth1" in cmd if isinstance(cmd, str) else False for cmd in cloud_init_config.get("runcmd", [])): - cloud_init_config["runcmd"].insert(0, "ip link set eth1 up") - user_data_yaml = "#cloud-config\n" + yaml.dump( cloud_init_config, default_flow_style=False ) @@ -179,10 +176,14 @@ class ProxmoxVMProvisionMixin: "ide2": f"{snippet_storage}:cloudinit", "citype": "nocloud", "cicustom": f"user={snippet_storage}:snippets/{filename}", - "ipconfig0": "ip=dhcp", # net0 gets DHCP (mgmt) - # NO ipconfig1 for net1 (capture NIC stays unnummeriert) } + # Configure DHCP for NICs where enabled (default True for index 0, False otherwise) + for i, nic in enumerate(nics): + dhcp_enabled = nic.get("dhcp", i == 0) # Default DHCP for first NIC only + if dhcp_enabled: + cloud_init_args[f"ipconfig{i}"] = "ip=dhcp" + if ssh_public_keys: # URL-encode SSH keys for Proxmox API sshkeys = ";".join(ssh_public_keys) @@ -190,7 +191,28 @@ class ProxmoxVMProvisionMixin: self._node_api().qemu(vmid).config.post(**cloud_init_args) - # Step 7: Start the VM + # Step 7: Optionally resize root disk + if disk_resize_gb is not None: + _logger.info(f"Resizing root disk to {disk_resize_gb}GB") + # Find root disk (scsi0, virtio0, ide0, sata0 — whichever is first) + try: + config = self._node_api().qemu(vmid).config.get() + root_disk = None + for prefix in ("scsi", "virtio", "ide", "sata"): + if f"{prefix}0" in config: + root_disk = f"{prefix}0" + break + if root_disk: + self._node_api().qemu(vmid).resize.put( + disk=root_disk, + size=f"{disk_resize_gb}G", + ) + else: + _logger.warning(f"Could not find root disk for VM {vmid}, skipping resize") + except Exception as e: + _logger.warning(f"Failed to resize disk: {e}, continuing anyway") + + # Step 8: Start the VM _logger.info(f"Starting VM {vmid}") start_upid = self._node_api().qemu(vmid).status.start.post() self._wait_for_task(start_upid, timeout=timeout)