Advertisement
P22DX

TermuxAlpine.bash

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