User Tools

Site Tools


Action unknown: copypageplugin__copy
proxmox:ct_op_basis_van_csv

Table of Contents

titel

context

dit document toont een (ChatGPT) script waarbij je containers cloont op basis van

  • een template (ID:2000)
  • een .CSV bestand:
    • VLANID
    • VLAN naam

script

#!/bin/bash

# Template/container ID to clone
TEMPLATE_ID=2000

# CSV file path
CSV_FILE="vlans.csv"

# Prefix for VLAN IDs <100
VMID_PREFIX=1

# Minimum free space warning threshold for thin pool (in %)
THIN_POOL_WARNING=90
THIN_POOL="pve/data"

# Function to find next available VMID
get_next_vmid() {
    local start=$1
    while pct status "$start" &>/dev/null; do
        start=$((start + 1))
    done
    echo "$start"
}

# Function to check thin pool usage
check_thin_pool() {
    local usage
    usage=$(lvs --noheadings -o data_percent "$THIN_POOL" | tr -d ' %')
    if [ "$usage" -ge "$THIN_POOL_WARNING" ]; then
        echo "ERROR: Thin pool $THIN_POOL is ${usage}% full. Aborting to prevent overcommit."
        exit 1
    fi
}

# Loop through CSV, skip header
tail -n +2 "$CSV_FILE" | while IFS=',' read -r vlanID vlanname; do
    # Check thin pool usage before each clone
    check_thin_pool

    # Ensure VMID is ≥100
    if [ "$vlanID" -lt 100 ]; then
        tentative_vmid="${VMID_PREFIX}${vlanID}"
    else
        tentative_vmid="$vlanID"
    fi

    # Get the next available VMID if tentative_vmid is taken
    VMID=$(get_next_vmid "$tentative_vmid")

    # Sanitize hostname: replace underscores with hyphens
    SAFE_HOSTNAME=$(echo "$vlanname" | tr '_' '-')

    echo "Cloning template $TEMPLATE_ID to VMID $VMID with hostname $SAFE_HOSTNAME (original VLAN name: $vlanname)"

    # Clone the container
    pct clone "$TEMPLATE_ID" "$VMID" --hostname "$SAFE_HOSTNAME" --full 1

    # Configure network after cloning
    pct set "$VMID" --net0 name=eth0,bridge=vmbr0,tag="$vlanID",ip=dhcp

    # Start the container
    pct start "$VMID"

    echo "Container $VMID ($SAFE_HOSTNAME) cloned and started successfully."
done

meer info

voeg hier linken toe naar verdere uitleg

proxmox/ct_op_basis_van_csv.txt · Last modified: by admin