You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.9 KiB

#!/usr/bin/env bash
# CONSTANTS
BASEDIR=$(dirname "$0")
CONTAINER_NAME=$1
TEMPLATE_DIR=/var/lib/vz/template/cache
TAG_NAME=debian13
SSH_PUBKEYS="$BASEDIR/pct-administrators.pubkeys"
# FUNCTIONS
function usage {
echo "$(basename "$0") <CONTAINER_NAME>"
}
function debian13_template {
ls -1 $TEMPLATE_DIR | grep ^debian-13
}
function show_defaults {
echo -n "STORAGE_DISK=$STORAGE_DISK,"
echo -n "STORAGE_SIZE=$STORAGE_SIZE,"
echo -n "MEMORY=$MEMORY,"
echo -n "SWAP=$SWAP,"
echo -n "CPU=$CPU,"
echo
}
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 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=vmbr0,ip=dhcp,firewall=1 \
--start \
--onboot true \
--ssh-public-keys $SSH_PUBKEYS \
--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")
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 "$BASEDIR/pct-default.config"
# show_defaults
existing_container && false || create_container