#!/usr/bin/env bash set -Eeuo pipefail trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR enabled "${TRACE:-}" && 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 timezone : "${KVM:="Y"}" # KVM acceleration : "${DEBUG:="N"}" # Disable debugging mode : "${COUNTRY:=""}" # Country code for mirror : "${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 # Sanitize variables TZ=$(strip "$TZ") STORAGE=$(strip "$STORAGE") COUNTRY=$(strip "$COUNTRY") DISK_SIZE=$(strip "$DISK_SIZE") # Helper variables ROOTLESS="N" PRIVILEGED="N" ENGINE="Docker" PROCESS="${APP,,}" PROCESS="${PROCESS// /-}" if [ -f "/run/.containerenv" ]; then ENGINE="${container:-}" if [[ "${ENGINE,,}" == *"podman"* ]]; then ROOTLESS="Y" ENGINE="Podman" else [ -z "$ENGINE" ] && ENGINE="Kubernetes" fi fi echo "❯ Starting $APP for $ENGINE v$(/dev/null && error "Invalid RAM_SIZE: $RAM_SIZE" && exit 16 wanted=$(numfmt --from=iec "$RAM_SIZE") [ "$wanted" -lt "$RAM_MINIMUM" ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16 fi # 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 KVM support if [[ "${PLATFORM,,}" == "x64" ]]; then TARGET="amd64" else TARGET="arm64" fi if disabled "$KVM"; then warn "KVM acceleration is disabled, this will cause the machine to run about 10 times slower!" else if [[ "${ARCH,,}" != "$TARGET" ]]; then KVM="N" warn "your CPU architecture is ${ARCH^^} and cannot provide KVM acceleration for ${PLATFORM^^} instructions, so the machine will run about 10 times slower." fi fi if ! disabled "$KVM"; then KVM_ERR="" if [ ! -e /dev/kvm ]; then KVM_ERR="(/dev/kvm is missing)" else if ! sh -c 'echo -n > /dev/kvm' &> /dev/null; then KVM_ERR="(/dev/kvm is unwriteable)" else if [[ "${PLATFORM,,}" == "x64" ]]; then flags=$(sed -ne '/^flags/s/^.*: //p' /proc/cpuinfo) if ! grep -qw "vmx\|svm" <<< "$flags"; then KVM_ERR="(not enabled in BIOS)" fi if ! grep -qw "sse4_2" <<< "$flags"; then error "Your CPU does not have the SSE4 instruction set that Virtual DSM requires!" ! enabled "$DEBUG" && exit 88 fi fi fi fi if [ -n "$KVM_ERR" ]; then KVM="N" if [[ "$OSTYPE" =~ ^darwin ]]; then warn "you are using macOS which has no KVM support, so the machine will run about 10 times slower." else kernel=$(uname -a) case "${kernel,,}" in *"microsoft"* ) error "Please bind '/dev/kvm' as a volume in the optional container settings when using Docker Desktop." ;; *"synology"* ) error "Please make sure that Synology VMM (Virtual Machine Manager) is installed and that '/dev/kvm' is binded to this container." ;; *) error "KVM acceleration is not available $KVM_ERR, this will cause the machine to run about 10 times slower." error "See the FAQ for possible causes, or disable acceleration by adding the \"KVM=N\" variable (not recommended)." ;; esac ! enabled "$DEBUG" && exit 88 fi fi fi # Cleanup files rm -f "$QEMU_DIR"/dsm.url rm -f "$QEMU_DIR"/qemu.* "$QEMU_DIR"/*.pid "$QEMU_DIR"/*.sock # Cleanup dirs rm -rf /tmp/dsm rm -rf "$STORAGE/tmp" return 0