#!/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 # If not running as root, don't hard-exit. Some restricted environments (like # Hugging Face Spaces) run containers without root privileges. Fall back to a # degraded non-root mode: warn, set NONROOT, and disable KVM and network # features that require capabilities. NONROOT="N" if [ "$(id -u)" -ne "0" ]; then warn "Script is not running as root. Entering non-root degraded mode. Some features (KVM, TAP/VHOST) will be disabled." NONROOT="Y" # Disable privileged features KVM="N" NETWORK="N" else NONROOT="N" fi # Docker environment variables : "${KVM:="Y"}" # KVM acceleration : "${BOOT:=""}" # Path of ISO file : "${DEBUG:="N"}" # Disable debugging : "${MACHINE:="q35"}" # Machine selection : "${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:="64G"}" # Initial data disk size : "${BOOT_MODE:=""}" # Boot system with UEFI : "${BOOT_INDEX:="9"}" # Boot index of CD drive : "${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 fi # Print system info SYS="${SYS/-generic/}" FS=$(stat -f -c %T "$STORAGE") 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 compatibilty if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then DISK_IO="threads" DISK_CACHE="writeback" fi if [[ "${BOOT_MODE:-}" == "windows"* ]]; then if [[ "${FS,,}" == "btrfs" ]]; then warn "you are using the BTRFS filesystem for /storage, this might introduce issues with Windows Setup!" fi fi # Check KVM support if [[ "${PLATFORM,,}" == "x64" ]]; then TARGET="amd64" else TARGET="arm64" fi if [[ "$KVM" == [Nn]* ]]; 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 [[ "$KVM" != [Nn]* ]]; 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 fi fi fi if [ -n "$KVM_ERR" ]; then # Do not hard-fail in restricted environments (e.g. Hugging Face Spaces). # Fall back to software acceleration (TCG) and continue startup. warn "KVM acceleration is not available $KVM_ERR, falling back to software TCG (will be slower)." KVM="N" # Provide guidance for users running locally or on supported hosts 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"* ) warn "If you want KVM on Docker Desktop, bind '/dev/kvm' as a volume in container settings." ;; *"synology"* ) warn "If you want KVM on Synology, enable VMM and bind '/dev/kvm' to the container." ;; *) warn "See the FAQ for possible causes, or set the environment variable KVM=N to force software acceleration." ;; esac fi fi fi return 0