From efe46e1fdc76a92c2e834e3b09b2fbcb2a59b23a Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 5 Dec 2023 06:56:20 +0100 Subject: [PATCH] feat: Mirror selection * feat: Country detection --- src/gpu.sh | 25 ++------------------ src/install.sh | 63 +++++++++++++++----------------------------------- src/reset.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 68 deletions(-) diff --git a/src/gpu.sh b/src/gpu.sh index fab3e2d..733d02f 100644 --- a/src/gpu.sh +++ b/src/gpu.sh @@ -18,28 +18,7 @@ fi chmod 666 /dev/dri/card0 chmod 666 /dev/dri/renderD128 -if ! apt-mark showinstall | grep -q "xserver-xorg-video-intel"; then - - info "Installing Intel GPU drivers..." - - export DEBCONF_NOWARNINGS="yes" - export DEBIAN_FRONTEND="noninteractive" - - apt-get -qq update - apt-get -qq --no-install-recommends -y install xserver-xorg-video-intel > /dev/null - -fi - -if ! apt-mark showinstall | grep -q "qemu-system-modules-opengl"; then - - info "Installing OpenGL module..." - - export DEBCONF_NOWARNINGS="yes" - export DEBIAN_FRONTEND="noninteractive" - - apt-get -qq update - apt-get -qq --no-install-recommends -y install qemu-system-modules-opengl > /dev/null - -fi +addPackage "xserver-xorg-video-intel" "Intel GPU drivers" +addPackage "qemu-system-modules-opengl" "OpenGL module" return 0 diff --git a/src/install.sh b/src/install.sh index 07164af..07ebe8f 100644 --- a/src/install.sh +++ b/src/install.sh @@ -14,15 +14,13 @@ fi [ -n "$URL" ] && BASE=$(basename "$URL" .pat) if [[ -f "$STORAGE/$BASE.boot.img" ]] && [[ -f "$STORAGE/$BASE.system.img" ]]; then - # Previous installation found - return 0 + return 0 # Previous installation found fi # Display wait message /run/server.sh 5000 install & DL="" -COUNTRY="" DL_CHINA="https://cndl.synology.cn/download/DSM" DL_GLOBAL="https://global.synologydownload.com/download/DSM" @@ -30,35 +28,11 @@ DL_GLOBAL="https://global.synologydownload.com/download/DSM" [[ "${URL,,}" == *"global.synology"* ]] && DL="$DL_GLOBAL" if [ -z "$DL" ]; then - - info "Install: Selecting download mirror..." - - # Detect country - { JSON=$(curl -H "Accept: application/json" -sfk https://ipinfo.io/json); rc=$?; } || : - - if (( rc == 0 )); then - { COUNTRY=$(echo "$JSON" | jq -r '.country' 2> /dev/null); rc=$?; } || : - (( rc != 0 )) || [[ "$COUNTRY" == "null" ]] && COUNTRY="" - fi - - if [[ -z "$COUNTRY" ]]; then - { JSON=$(curl -H "Accept: application/json" -sfk https://api.ipapi.is); rc=$?; } || : - if (( rc == 0 )); then - { COUNTRY=$(echo "$JSON" | jq -r '.location.country_code' 2> /dev/null); rc=$?; } || : - (( rc != 0 )) || [[ "$COUNTRY" == "null" ]] && COUNTRY="" - fi - fi - - # Select download mirror based on country - if [ "$COUNTRY" == "CN" ]; then - DL="$DL_CHINA" - else - DL="$DL_GLOBAL" - fi + [ -z "$COUNTRY" ] && setCountry + [[ "${COUNTRY^^}" == "CN" ]] && DL="$DL_CHINA" || DL="$DL_GLOBAL" fi if [ -z "$URL" ]; then - # Select default version based on architecture if [ "$ARCH" == "amd64" ]; then URL="$DL/release/7.2.1/69057-1/DSM_VirtualDSM_69057.pat" else @@ -75,13 +49,14 @@ fi BASE=$(basename "$URL" .pat) -rm -f "$STORAGE"/"$BASE".pat +if [[ "$URL" != "file://${STORAGE}/${BASE}.pat" ]]; then + rm -f "$STORAGE"/"$BASE".pat +fi + rm -f "$STORAGE"/"$BASE".agent rm -f "$STORAGE"/"$BASE".boot.img rm -f "$STORAGE"/"$BASE".system.img -info "Install: Checking filesystem..." - [[ "${DEBUG}" == [Yy1]* ]] && set -x # Check filesystem @@ -202,9 +177,17 @@ info "Install: Downloading $(basename "$URL")..." PAT="/$BASE.pat" rm -f "$PAT" -{ wget "$URL" -O "$PAT" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || : +if [[ "$URL" == "file://"* ]]; then + + cp "${URL:7}" "$PAT" + +else + + { wget "$URL" -O "$PAT" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || : + (( rc != 0 )) && error "Failed to download $URL, reason: $rc" && exit 69 + +fi -(( rc != 0 )) && error "Failed to download $URL, reason: $rc" && exit 69 [ ! -f "$PAT" ] && error "Failed to download $URL" && exit 69 SIZE=$(stat -c%s "$PAT") @@ -220,17 +203,7 @@ if { tar tf "$PAT"; } >/dev/null 2>&1; then else - if [ "$ARCH" != "amd64" ]; then - - info "Install: Installing QEMU..." - - export DEBCONF_NOWARNINGS="yes" - export DEBIAN_FRONTEND="noninteractive" - - apt-get -qq update - apt-get -qq --no-install-recommends -y install qemu-user > /dev/null - - fi + [ "$ARCH" != "amd64" ] && addPackage "qemu-user" "QEMU" info "Install: Extracting downloaded image..." diff --git a/src/reset.sh b/src/reset.sh index 19ff8bd..334fdd7 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -12,6 +12,7 @@ trap 'error "Status $? while: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR : ${GPU:='N'} # Enable GPU passthrough : ${DEBUG:='N'} # Enable debugging mode +: ${COUNTRY:=''} # Country code for mirror : ${CONSOLE:='N'} # Start in console mode : ${ALLOCATE:='Y'} # Preallocate diskspace : ${ARGUMENTS:=''} # Extra QEMU parameters @@ -42,4 +43,64 @@ rm -f /run/qemu.count rm -rf /tmp/dsm rm -rf "$STORAGE/tmp" +# Helper functions + +getCountry () { + + local rc + local json + local result + local url=$1 + local query=$2 + + { json=$(curl -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 () { + + [ -z "$COUNTRY" ] && getCountry "https://api.ipapi.is" ".location.country_code" + [ -z "$COUNTRY" ] && getCountry "https://ifconfig.co/json" ".country_iso" + [ -z "$COUNTRY" ] && getCountry "https://ipinfo.io/json" ".country" + [ -z "$COUNTRY" ] && getCountry "https://api.myip.com" ".cc" + + return 0 +} + +addPackage () { + + local pkg=$1 + local desc=$2 + + if apt-mark showinstall | grep -q "$pkg"; then + return 0 + fi + + info "Installing $desc..." + + export DEBCONF_NOWARNINGS="yes" + export DEBIAN_FRONTEND="noninteractive" + + [ -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 + + apt-get -qq update + apt-get -qq --no-install-recommends -y install "$pkg" > /dev/null + + return 0 +} + return 0