P22DX

TermuxAlpine.bash

Dec 17th, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.66 KB | None | 0 0
  1. #!/data/data/com.termux/files/usr/bin/bash -e
  2. # Copyright ©2019 by Hax4Us. All rights reserved.
  3. #
  4. ################################################################################
  5.  
  6. # colors
  7.  
  8. red='\033[1;31m'
  9. yellow='\033[1;33m'
  10. blue='\033[1;34m'
  11. reset='\033[0m'
  12.  
  13.  
  14. # Destination Path
  15.  
  16. DESTINATION=${PREFIX}/share/TermuxAlpine
  17. choice=""
  18. if [ -d ${DESTINATION} ]; then
  19.     printf "${red}[!] ${yellow}Alpine is already installed\nDo you want to reinstall ? (type \"y\" for yes or \"n\" for no) :${reset} "
  20.     read choice
  21.     if [ "${choice}" = "y" ]; then
  22.         rm -rf ${DESTINATION}
  23.     elif [ "${choice}" = "n" ]; then
  24.         exit 1
  25.     else
  26.         printf "${red}[!] Wrong input${reset}"
  27.         exit 1
  28.     fi
  29.  
  30. fi
  31. mkdir ${DESTINATION}
  32. cd ${DESTINATION}
  33.  
  34. # Utility function for Unknown Arch
  35.  
  36. unknownarch() {
  37.     printf "$red"
  38.     echo "[*] Unknown Architecture :("
  39.     printf "$reset"
  40.     exit 1
  41. }
  42.  
  43. # Utility function for detect system
  44.  
  45. checksysinfo() {
  46.     printf "$blue [*] Checking host architecture ..."
  47.     case $(getprop ro.product.cpu.abi) in
  48.         arm64-v8a)
  49.             SETARCH=aarch64
  50.             ;;
  51.         armeabi|armeabi-v7a)
  52.             SETARCH=armhf
  53.             ;;
  54.         x86|i686)
  55.             SETARCH=x86
  56.             ;;
  57.         x86_64)
  58.             SETARCH=x86_64
  59.             ;;
  60.         *)
  61.             unknownarch
  62.             ;;
  63.     esac
  64. }
  65.  
  66. # Check if required packages are present
  67.  
  68. checkdeps() {
  69.     printf "${blue}\n"
  70.     echo " [*] Updating apt cache..."
  71.     apt update -y &> /dev/null
  72.     echo " [*] Checking for all required tools..."
  73.  
  74.     for i in proot bsdtar curl; do
  75.         if [ -e ${PREFIX}/bin/$i ]; then
  76.             echo " • $i is OK"
  77.         else
  78.             echo "Installing ${i}..."
  79.             apt install -y $i || {
  80.                 printf "$red"
  81.                 echo " ERROR: check your internet connection or apt\n Exiting..."
  82.                 printf "$reset"
  83.                 exit 1
  84.             }
  85.         fi
  86.     done
  87. }
  88.  
  89. # URLs of all possibls architectures
  90.  
  91. seturl() {
  92.     ALPINE_VER=$(curl -s http://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/$SETARCH/latest-releases.yaml | grep -m 1 -o version.* | sed -e 's/[^0-9.]*//g' -e 's/-$//')
  93.     if [ -z "$ALPINE_VER" ] ; then
  94.         exit 1
  95.     fi
  96.     ALPINE_URL="http://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/$SETARCH/alpine-minirootfs-$ALPINE_VER-$SETARCH.tar.gz"
  97. }
  98.  
  99. # Utility function to get tar file
  100.  
  101. gettarfile() {
  102.     printf "$blue [*] Getting tar file...$reset\n\n"
  103.     seturl $SETARCH
  104.     curl --progress-bar -L --fail --retry 4 -O "$ALPINE_URL"
  105.     rootfs="alpine-minirootfs-$ALPINE_VER-$SETARCH.tar.gz"
  106. }
  107.  
  108. # Utility function to get SHA
  109.  
  110. getsha() {
  111.     printf "\n${blue} [*] Getting SHA ... $reset\n\n"
  112.     curl --progress-bar -L --fail --retry 4 -O "${ALPINE_URL}.sha256"
  113. }
  114.  
  115. # Utility function to check integrity
  116.  
  117. checkintegrity() {
  118.     printf "\n${blue} [*] Checking integrity of file...\n"
  119.     echo " [*] The script will immediately terminate in case of integrity failure"
  120.     printf ' '
  121.     sha256sum -c ${rootfs}.sha256 || {
  122.         printf "$red Sorry :( to say your downloaded linux file was corrupted or half downloaded, but don't worry, just rerun my script\n${reset}"
  123.         exit 1
  124.     }
  125. }
  126.  
  127. # Utility function to extract tar file
  128.  
  129. extract() {
  130.     printf "$blue [*] Extracting... $reset\n\n"
  131.     proot --link2symlink -0 bsdtar -xpf $rootfs 2> /dev/null || :
  132. }
  133.  
  134. # Utility function for login file
  135.  
  136. createloginfile() {
  137.     bin=${PREFIX}/bin/startalpine
  138.     cat > $bin <<- EOM
  139. #!/data/data/com.termux/files/usr/bin/bash -e
  140. unset LD_PRELOAD
  141. # thnx to @j16180339887 for DNS picker
  142. addresolvconf ()
  143. {
  144.   android=\$(getprop ro.build.version.release)
  145.   if [ \${android%%.*} -lt 8 ]; then
  146.   [ \$(command -v getprop) ] && getprop | sed -n -e 's/^\[net\.dns.\]: \[\(.*\)\]/\1/p' | sed '/^\s*$/d' | sed 's/^/nameserver /' > \${PREFIX}/share/TermuxAlpine/etc/resolv.conf
  147.   fi
  148. }
  149. addresolvconf
  150. exec proot --link2symlink -0 -r \${PREFIX}/share/TermuxAlpine/ -b /dev/ -b /sys/ -b /proc/ -b /sdcard -b /storage -b \$HOME -w /home /usr/bin/env HOME=/root PREFIX=/usr SHELL=/bin/sh TERM="\$TERM" LANG=\$LANG PATH=/bin:/usr/bin:/sbin:/usr/sbin /bin/sh --login
  151. EOM
  152.  
  153.     chmod 700 $bin
  154. }
  155.  
  156. # Utility function to touchup Alpine
  157.  
  158. finalwork() {
  159.     [ ! -e ${DESTINATION}/finaltouchup.sh ] && curl --silent -LO https://raw.githubusercontent.com/Hax4us/TermuxAlpine/master/finaltouchup.sh
  160.     if [ "${MOTD}" = "ON" ]; then
  161.         bash ${DESTINATION}/finaltouchup.sh --add-motd
  162.     else
  163.         bash ${DESTINATION}/finaltouchup.sh
  164.     fi
  165.     rm ${DESTINATION}/finaltouchup.sh
  166. }
  167.  
  168.  
  169.  
  170. # Utility function for cleanup
  171.  
  172. cleanup() {
  173.     if [ -d ${DESTINATION} ]; then
  174.         rm -rf ${DESTINATION}
  175.     else
  176.         printf "$red not installed so not removed${reset}\n"
  177.         exit
  178.     fi
  179.     if [ -e ${PREFIX}/bin/startalpine ]; then
  180.         rm ${PREFIX}/bin/startalpine
  181.         printf "$yellow uninstalled :) ${reset}\n"
  182.         exit
  183.     else
  184.         printf "$red not installed so not removed${reset}\n"
  185.     fi
  186. }
  187.  
  188. printline() {
  189.     printf "${blue}\n"
  190.     echo " #------------------------------------------#"
  191. }
  192.  
  193. usage() {
  194.     printf "${yellow}\nUsage: ${green}bash TermuxAlpine.sh [option]\n${blue}  --uninstall       uninstall alpine\n  --add-motd      create motd file\n${reset}\n"
  195. }
  196.  
  197. # Start
  198.  
  199. MOTD="OFF"
  200. EXTRAARGS="default"
  201. if [ ! -z "$1" ]
  202.     then
  203.     EXTRAARGS=$1
  204. fi
  205. if [ "$EXTRAARGS" = "--uninstall" ]; then
  206.     cleanup
  207.     exit 1
  208. elif [ "$EXTRAARGS" = "--add-motd"  ]; then
  209.     MOTD="ON"
  210. elif [ $# -ge 1 ]
  211. then
  212.     usage
  213.     exit 1
  214. fi
  215. printf "\n${yellow} You are going to install Alpine in termux ;) Cool\n press ENTER to continue\n"
  216. read enter
  217.  
  218. checksysinfo
  219. checkdeps
  220. gettarfile
  221. getsha
  222. checkintegrity
  223. extract
  224. createloginfile
  225.  
  226. printf "$blue [*] Configuring Alpine For You ..."
  227. finalwork
  228. printline
  229. printf "\n${yellow} Now you can enjoy a very small (just 1 MB!) Linux environment in your Termux :)\n Don't forget to star my work\n"
  230. printline
  231. printline
  232. printf "\n${blue} [*] Email   :${yellow}    [email protected]\n"
  233. printf "$blue [*] Website :${yellow}    https://hax4us.com\n"
  234. printf "$blue [*] YouTube :${yellow}    https://youtube.com/hax4us\n"
  235. printline
  236. printf "$red \n NOTE : $yellow use ${red}--uninstall${yellow} option for uninstall\n"
  237. printline
  238. printf "$reset"
Advertisement
Add Comment
Please, Sign In to add comment