LXC container build and configuration functions for Proxmox VE
The build.func library provides the main build functions for creating and configuring LXC containers in Proxmox VE. It handles variable initialization, container creation, resource allocation, storage selection, and advanced configuration.
validate_container_id() { local ctid="$1" # Check if ID is numeric if ! [[ "$ctid" =~ ^[0-9]+$ ]]; then return 1 fi # CLUSTER-WIDE CHECK: Query all VMs/CTs across all nodes if command -v pvesh &>/dev/null; then local cluster_ids cluster_ids=$(pvesh get /cluster/resources --type vm --output-format json 2>/dev/null | grep -oP '"vmid":\s*\K[0-9]+' 2>/dev/null || true) if [[ -n "$cluster_ids" ]] && echo "$cluster_ids" | grep -qw "$ctid"; then return 1 fi fi # LOCAL FALLBACK: Check if config file exists if [[ -f "/etc/pve/qemu-server/${ctid}.conf" ]] || [[ -f "/etc/pve/lxc/${ctid}.conf" ]]; then return 1 fi # Check if ID is used in LVM logical volumes if lvs --noheadings -o lv_name 2>/dev/null | grep -qE "(^|[-_])${ctid}($|[-_])"; then return 1 fi return 0}
validate_hostname() { local hostname="$1" # Check total length (max 253 for FQDN) if [[ ${#hostname} -gt 253 ]] || [[ -z "$hostname" ]]; then return 1 fi # Split by dots and validate each label local IFS='.' read -ra labels <<<"$hostname" for label in "${labels[@]}"; do # Each label: 1-63 chars, alphanumeric, hyphens allowed (not at start/end) if [[ -z "$label" ]] || [[ ${#label} -gt 63 ]]; then return 1 fi if [[ ! "$label" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]] && [[ ! "$label" =~ ^[a-z0-9]$ ]]; then return 1 fi done return 0}
validate_ip_address() { local ip="$1" [[ -z "$ip" ]] && return 1 # Check format with CIDR if [[ ! "$ip" =~ ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/([0-9]{1,2})$ ]]; then return 1 fi local o1="${BASH_REMATCH[1]}" local o2="${BASH_REMATCH[2]}" local o3="${BASH_REMATCH[3]}" local o4="${BASH_REMATCH[4]}" local cidr="${BASH_REMATCH[5]}" # Validate octets (0-255) for octet in "$o1" "$o2" "$o3" "$o4"; do if ((octet > 255)); then return 1 fi done # Validate CIDR (1-32) if ((cidr < 1 || cidr > 32)); then return 1 fi return 0}
Defines all base/default variables for container creation.
Configuration Variables Set
Container Type: Privileged (0) or Unprivileged (1)
Resources: CPU cores, RAM size, disk size
Network: Bridge, IP address, IPv6 method, gateway
SSH: SSH access and authorized keys
Features: Nesting, keyctl, mknod, FUSE, TUN
Tags: Proxmox tags for organization
build.func:883-1015
base_settings() { # Container type CT_TYPE=${var_unprivileged:-"1"} # Resource allocation: App defaults take precedence if HIGHER local final_disk="${var_disk:-4}" local final_cpu="${var_cpu:-1}" local final_ram="${var_ram:-1024}" # If app declared higher values, use those instead if [[ -n "${APP_DEFAULT_DISK:-}" && "${APP_DEFAULT_DISK}" =~ ^[0-9]+$ ]]; then if [[ "${APP_DEFAULT_DISK}" -gt "${final_disk}" ]]; then final_disk="${APP_DEFAULT_DISK}" fi fi DISK_SIZE="${final_disk}" CORE_COUNT="${final_cpu}" RAM_SIZE="${final_ram}" # Validate and set Container ID local requested_id="${var_ctid:-$NEXTID}" if ! validate_container_id "$requested_id"; then requested_id=$(get_valid_container_id "$requested_id") fi CT_ID="$requested_id" # Network configuration BRG=${var_brg:-"vmbr0"} NET=${var_net:-"dhcp"} IPV6_METHOD=${var_ipv6_method:-"none"} GATE=${var_gateway:-""} # SSH and features SSH=${var_ssh:-"no"} TAGS="community-script,${var_tags:-}" ENABLE_NESTING=${var_nesting:-"1"}}
resolve_ip_from_range() { local range="$1" local ip_start ip_end # Parse range: "10.0.0.1/24-10.0.0.10/24" ip_start="${range%%-*}" ip_end="${range##*-}" local ip1="${ip_start%%/*}" local ip2="${ip_end%%/*}" local cidr="${ip_start##*/}" local start_int=$(ip_to_int "$ip1") local end_int=$(ip_to_int "$ip2") for ((ip_int = start_int; ip_int <= end_int; ip_int++)); do local ip=$(int_to_ip $ip_int) msg_info "Checking IP: $ip" if ! ping -c 1 -W 1 "$ip" >/dev/null 2>&1; then NET_RESOLVED="$ip/$cidr" msg_ok "Found free IP: ${BGN}$NET_RESOLVED${CL}" return 0 fi done NET_RESOLVED="" msg_error "No free IP found in range $range" return 1}