#!/usr/bin/env bash # CONSTANTS BASEDIR=$(dirname "$0") CONTAINER_NAME=$1 TEMPLATE_DIR=/var/lib/vz/template/cache TAG_NAME=debian13 SSH_PUBKEYS="$BASEDIR/../config/admin.pubkeys" CONFIG_FILE="$BASEDIR/../config/default.conf" # FUNCTIONS function usage { echo "$(basename "$0") " } function debian13_template { ls -1 $TEMPLATE_DIR | grep ^debian-13 } function convert_human_size_to_megabyte { value=$1 echo "$(numfmt --from=iec-i "$value"i --to-unit=Mi)" | cut -dM -f1 } function convert_human_size_to_gigabyte { value=$1 echo "$(numfmt --from=iec-i "$value"i --to-unit=Gi)" | cut -dG -f1 } function optional_public_keys { if [[ -f $SSH_PUBKEYS ]]; then echo "--ssh-public-keys $SSH_PUBKEYS" else echo fi } function create_container { local new_id=$($BASEDIR/pct-nextid) local template_file="$TEMPLATE_DIR/$(debian13_template)" local vol_in_gb=$(convert_human_size_to_gigabyte $STORAGE_SIZE) local mem_in_mb=$(convert_human_size_to_megabyte $MEMORY) local swp_in_mb=$(convert_human_size_to_megabyte $SWAP) pct create \ $new_id \ $template_file \ --ostype debian \ --rootfs "volume=$STORAGE_DISK:$vol_in_gb" \ --cores $CPU \ --memory $mem_in_mb \ --swap $swp_in_mb \ --hostname $CONTAINER_NAME \ --net0 name=eth0,bridge=$BRIDGE,ip=dhcp,firewall=1 \ --start \ --onboot true \ $(optional_public_keys) \ --unprivileged true \ --features nesting=1 \ [[ -n $TAG_NAME ]] && pct set $new_id --tags $TAG_NAME echo "container: $CONTAINER_NAME succesfully created with vmid=$new_id!" } function existing_container { local vmid=$($BASEDIR/pct-lookup "$CONTAINER_NAME" 2>/dev/null) if [[ -z $vmid ]]; then false else echo "container $CONTAINER_NAME already exists with vmid=$vmid!" exit 2 fi } # MAIN set -Eue [[ "$#" -lt 1 ]] && usage && exit 1 source "$CONFIG_FILE" existing_container && false || create_container