User Tools

Site Tools


proxmox:ct_op_basis_van_csv

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
proxmox:ct_op_basis_van_csv [2025/11/23 15:58] – [context] adminproxmox:ct_op_basis_van_csv [2025/11/23 16:05] (current) admin
Line 18: Line 18:
 CSV_FILE="vlans.csv" CSV_FILE="vlans.csv"
  
-# Prefix to add to VLAN IDs below 100+# Prefix for VLAN IDs <100
 VMID_PREFIX=1 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 # Loop through CSV, skip header
 tail -n +2 "$CSV_FILE" | while IFS=',' read -r vlanID vlanname; do tail -n +2 "$CSV_FILE" | while IFS=',' read -r vlanID vlanname; do
-    # Ensure VMID is >= 100+    # Check thin pool usage before each clone 
 +    check_thin_pool 
 + 
 +    # Ensure VMID is 100
     if [ "$vlanID" -lt 100 ]; then     if [ "$vlanID" -lt 100 ]; then
-        VMID="${VMID_PREFIX}${vlanID}"+        tentative_vmid="${VMID_PREFIX}${vlanID}"
     else     else
-        VMID="$vlanID"+        tentative_vmid="$vlanID"
     fi     fi
  
-    echo "Cloning container from template $TEMPLATE_ID with new VMID $VMID and hostname $vlanname"+    # 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     # Clone the container
-    pct clone "$TEMPLATE_ID" "$VMID" --hostname "$vlanname" --full 1+    pct clone "$TEMPLATE_ID" "$VMID" --hostname "$SAFE_HOSTNAME" --full 1
  
     # Configure network after cloning     # Configure network after cloning
Line 41: Line 73:
     pct start "$VMID"     pct start "$VMID"
  
-    echo "Container $VMID ($vlanname) cloned and started successfully."+    echo "Container $VMID ($SAFE_HOSTNAME) cloned and started successfully."
 done done
 +
 </code> </code>
 ===== meer info ===== ===== meer info =====
proxmox/ct_op_basis_van_csv.1763913526.txt.gz · Last modified: by admin