#!/usr/bin/env bash set -Eeuo pipefail trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR [[ "${TRACE:-}" == [Yy1]* ]] && set -o functrace && trap 'echo "# $BASH_COMMAND" >&2' DEBUG [ ! -f "/run/entry.sh" ] && error "Script must be run inside the container!" && exit 11 [ "$(id -u)" -ne "0" ] && error "Script must be executed with root privileges." && exit 12 # Docker environment variables : "${TZ:=""}" # System local timezone : "${DEBUG:="N"}" # Disable debugging mode : "${COUNTRY:=""}" # Country code for mirror : "${CONSOLE:="N"}" # Disable console mode : "${ALLOCATE:=""}" # Preallocate diskspace : "${ARGUMENTS:=""}" # Extra QEMU parameters : "${CPU_CORES:="2"}" # Amount of CPU cores : "${RAM_SIZE:="2G"}" # Maximum RAM amount : "${RAM_CHECK:="Y"}" # Check available RAM : "${DISK_SIZE:="16G"}" # Initial data disk size : "${STORAGE:="/storage"}" # Storage folder location # Helper variables PODMAN="N" ENGINE="Docker" PROCESS="${APP,,}" PROCESS="${PROCESS// /-}" if [ -f "/run/.containerenv" ]; then PODMAN="Y" ENGINE="Podman" fi echo "❯ Starting $APP for $ENGINE v$(/dev/null && error "Invalid RAM_SIZE: $RAM_SIZE" && exit 16 RAM_WANTED=$(numfmt --from=iec "$RAM_SIZE") [ "$RAM_WANTED" -lt "136314880 " ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16 # Print system info SYS="${SYS/-generic/}" FS="${FS/UNKNOWN //}" FS="${FS/ext2\/ext3/ext4}" FS=$(echo "$FS" | sed 's/[)(]//g') SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1) SPACE_GB=$(formatBytes "$SPACE" "down") AVAIL_MEM=$(formatBytes "$RAM_AVAIL" "down") TOTAL_MEM=$(formatBytes "$RAM_TOTAL" "up") echo "❯ CPU: ${CPU} | RAM: ${AVAIL_MEM/ GB/}/$TOTAL_MEM | DISK: $SPACE_GB (${FS}) | KERNEL: ${SYS}..." echo # Check available memory if [[ "$RAM_CHECK" != [Nn]* ]] && (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then AVAIL_MEM=$(formatBytes "$RAM_AVAIL") msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available, please set a lower value." [[ "${FS,,}" != "zfs" ]] && error "$msg" && exit 17 info "$msg" fi # Cleanup files rm -f /run/shm/qemu.* rm -f /run/shm/dsm.url # Cleanup dirs rm -rf /tmp/dsm rm -rf "$STORAGE/tmp" getCountry() { local url=$1 local query=$2 local rc json result { json=$(curl -m 5 -H "Accept: application/json" -sfk "$url"); rc=$?; } || : (( rc != 0 )) && return 0 { result=$(echo "$json" | jq -r "$query" 2> /dev/null); rc=$?; } || : (( rc != 0 )) && return 0 [[ ${#result} -ne 2 ]] && return 0 [[ "${result^^}" == "XX" ]] && return 0 COUNTRY="${result^^}" return 0 } setCountry() { [[ "${TZ,,}" == "asia/harbin" ]] && COUNTRY="CN" [[ "${TZ,,}" == "asia/beijing" ]] && COUNTRY="CN" [[ "${TZ,,}" == "asia/urumqi" ]] && COUNTRY="CN" [[ "${TZ,,}" == "asia/kashgar" ]] && COUNTRY="CN" [[ "${TZ,,}" == "asia/shanghai" ]] && COUNTRY="CN" [[ "${TZ,,}" == "asia/chongqing" ]] && COUNTRY="CN" [ -z "$COUNTRY" ] && getCountry "https://api.ipapi.is" ".location.country_code" [ -z "$COUNTRY" ] && getCountry "https://ifconfig.co/json" ".country_iso" [ -z "$COUNTRY" ] && getCountry "https://api.ip2location.io" ".country_code" [ -z "$COUNTRY" ] && getCountry "https://ipinfo.io/json" ".country" [ -z "$COUNTRY" ] && getCountry "https://api.ipquery.io/?format=json" ".location.country_code" [ -z "$COUNTRY" ] && getCountry "https://api.myip.com" ".cc" return 0 } addPackage() { local pkg=$1 local desc=$2 if apt-mark showinstall | grep -qx "$pkg"; then return 0 fi MSG="Installing $desc..." info "$MSG" && html "$MSG" [ -z "$COUNTRY" ] && setCountry if [[ "${COUNTRY^^}" == "CN" ]]; then sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources fi DEBIAN_FRONTEND=noninteractive apt-get -qq update DEBIAN_FRONTEND=noninteractive apt-get -qq --no-install-recommends -y install "$pkg" > /dev/null return 0 } : "${COM_PORT:="2210"}" # Comm port : "${MON_PORT:="7100"}" # Monitor port : "${WEB_PORT:="5000"}" # Webserver port : "${CHR_PORT:="12345"}" # Character port cp -r /var/www/* /run/shm html "Starting $APP for $ENGINE..." if [[ "${WEB:-}" != [Nn]* ]]; then mkdir -p /etc/nginx/sites-enabled cp /etc/nginx/default.conf /etc/nginx/sites-enabled/web.conf sed -i "s/listen 5000 default_server;/listen $WEB_PORT default_server;/g" /etc/nginx/sites-enabled/web.conf # shellcheck disable=SC2143 if [ -f /proc/net/if_inet6 ] && [ -n "$(ifconfig -a | grep inet6)" ]; then sed -i "s/listen $WEB_PORT default_server;/listen [::]:$WEB_PORT default_server ipv6only=off;/g" /etc/nginx/sites-enabled/web.conf fi # Start webserver nginx -e stderr fi return 0