Advertisement
Guest User

install-linuxcontainers.sh

a guest
Nov 3rd, 2021
185
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/system/bin/sh
  2.  
  3. # Different linux rootfs archives from linuxcontainers.org install script.
  4.  
  5. set -e
  6.  
  7. ###
  8. show_usage() {
  9. echo 'Usage:'
  10. echo " $0 [-a] [-d] <distro> <release> [<target_subdir_name>]"
  11. echo ' -a -- non-interactive mode'
  12. echo ' -d -- do not use minitar and PRoot from a plugin if present'
  13. echo
  14. echo 'Variables:'
  15. echo ' REG_USER - user account name; default:' "$REG_USER"
  16. echo ' FAV_SHELL - preferable shell; default:' "$FAV_SHELL" '(fallback: /bin/sh)'
  17. echo ' PROOT - proot location; default: <auto>'
  18. echo ' PROOT_USERLAND - proot userland flavor location; default: <auto>'
  19. echo ' ESSENTIALS_PKG - Application ID of a minitar and PRoot plugin to check; default:' "$ESSENTIALS"
  20. echo ' ROOTFS_URL - override root FS source URL'
  21. }
  22. ###
  23.  
  24. # We can't simply use `()' to introduce newly exported TMPDIR to the shell in Android 10.
  25. _TMPDIR="$DATA_DIR/tmp"
  26. if [ "$_TMPDIR" != "$TMPDIR" ] ; then
  27. export TMPDIR="$_TMPDIR"
  28. mkdir -p "$TMPDIR"
  29. /system/bin/sh "$0" "$@"
  30. exit "$?"
  31. fi
  32. export TMPDIR
  33. mkdir -p "$TMPDIR"
  34.  
  35. trap 'exit 1' INT HUP QUIT TERM ALRM USR1
  36.  
  37. TERMSH="$LIB_DIR/libtermsh.so"
  38.  
  39. if [ "$1" = '-a' ] ; then
  40. NI=1
  41. shift
  42. else
  43. NI=
  44. fi
  45.  
  46. DISTRO="$1"
  47. RELEASE="$2"
  48. NAME="${3:-"linuxcontainers-$DISTRO-$RELEASE"}"
  49. REG_USER="${REG_USER:-my_acct}"
  50. FAV_SHELL="${FAV_SHELL:-/bin/bash}"
  51. PROOT="${PROOT:-'$DATA_DIR/root/bin/proot'}"
  52. PROOT_USERLAND="${PROOT:-'$DATA_DIR/root/bin/proot-userland'}"
  53. ESSENTIALS="${ESSENTIALS_PKG:-green_green_avk.anothertermshellplugin_android10essentials}"
  54.  
  55. if [ -z "$1" -o -z "$2" ] ; then
  56. show_usage
  57. exit 1
  58. fi
  59.  
  60. find_prefix() { # Old Androids have no `grep'.
  61. local L
  62. while read -r L ; do
  63. case $L in
  64. $1*) echo "$L" ; return 0 ;;
  65. esac
  66. done
  67. return 1
  68. }
  69.  
  70. exit_with() {
  71. echo "$@" >&2
  72. exit 1
  73. }
  74.  
  75. prompt() {
  76. echo -en "\e[1m$1 [\e[0m$2\e[1m]:\e[0m "
  77. local _V
  78. read _V
  79. _V="${_V:-"$2"}"
  80. eval "$3=${_V@Q}" # Default shell can't `typeset -g' before Android 8.
  81. }
  82.  
  83. to_uname_arch() {
  84. case "$1" in
  85. armeabi-v7a) echo armv7a ;;
  86. arm64-v8a) echo aarch64 ;;
  87. x86) echo i686 ;;
  88. amd64) echo x86_64 ;;
  89. *) echo "$1" ;;
  90. esac
  91. }
  92.  
  93. validate_arch() {
  94. case "$1" in
  95. armv7a|aarch64|i686|amd64) echo $1 ; return 0 ;;
  96. *) return 1 ;;
  97. esac
  98. }
  99.  
  100. validate_dir() { [ -d "$1" -a -r "$1" -a -w "$1" -a -x "$1" ] ; }
  101.  
  102. PROOTS='proots'
  103.  
  104. if [ -z "$NI" ] ; then
  105. NAME="linuxcontainers-$DISTRO-$RELEASE"
  106. echo
  107. prompt "Installation subdir name $PROOTS/___" "$NAME" NAME
  108. fi
  109.  
  110. mkdir -p "$DATA_DIR/$PROOTS"
  111. if ! validate_dir "$DATA_DIR/$PROOTS" ; then
  112. echo -e "\nUnable to create \$DATA_DIR/$PROOTS"
  113. exit 1
  114. fi
  115.  
  116. NAME_C=1
  117. NAME_S=
  118. NAME_B="$NAME"
  119. while true ; do
  120. NAME="$NAME_B$NAME_S"
  121. ROOTFS_DIR="$PROOTS/$NAME"
  122. if mkdir "$DATA_DIR/$ROOTFS_DIR" >/dev/null 2>&1 ; then break ; fi
  123. if [ "$NAME_C" -gt 100 ] ; then
  124. echo -e '\nSuspiciously many rootfses installed'
  125. exit 1
  126. fi
  127. NAME_C="$(($NAME_C+1))"
  128. NAME_S="-$NAME_C"
  129. done
  130.  
  131. echo -e "\nActual name: $NAME\n"
  132. echo -e "To uninstall: run \`rm -rf \"\$DATA_DIR/$ROOTFS_DIR\"'\n"
  133.  
  134. MINITAR="$DATA_DIR/minitar"
  135.  
  136.  
  137. echo 'Creating favorites...'
  138.  
  139. echo -e '#!/system/bin/sh\n\necho Installing... Try later.' > "$DATA_DIR/$ROOTFS_DIR/run"
  140. chmod 755 "$DATA_DIR/$ROOTFS_DIR/run"
  141.  
  142. case "$DISTRO" in
  143. alpine) RUN_OPTS_TERM='xterm-xfree86' ;;
  144. *) RUN_OPTS_TERM='' ;;
  145. esac
  146.  
  147. RUN="/system/bin/sh \"\$DATA_DIR/$ROOTFS_DIR/run\""
  148.  
  149. if [ -z "$NI" ] ; then
  150.  
  151. if [ -n "$RUN_OPTS_TERM" ] ; then
  152. RUN_OPTS="&terminal_string=$RUN_OPTS_TERM"
  153. else
  154. RUN_OPTS=''
  155. fi
  156. UE_RUN="$("$TERMSH" uri-encode "$RUN")"
  157. "$TERMSH" view \
  158. -r 'green_green_avk.anotherterm.FavoriteEditorActivity' \
  159. -u "local-terminal:/opts?execute=${UE_RUN}%200%3A0&name=$("$TERMSH" uri-encode "$NAME (root)")$RUN_OPTS"
  160. "$TERMSH" view \
  161. -r 'green_green_avk.anotherterm.FavoriteEditorActivity' \
  162. -u "local-terminal:/opts?execute=${UE_RUN}&name=$("$TERMSH" uri-encode "$NAME")$RUN_OPTS"
  163.  
  164. else
  165.  
  166. "$TERMSH" request-permission favmgmt 'Installer is going to create a regular user and a root launching favs.' \
  167. && {
  168. finally() { "$TERMSH" revoke-permission favmgmt ; trap - EXIT ; unset finally ; }
  169. trap 'finally' EXIT
  170. } || [ $? -eq 3 ]
  171. if [ -n "$RUN_OPTS_TERM" ] ; then
  172. RUN_OPTS=(-t "$RUN_OPTS_TERM")
  173. else
  174. RUN_OPTS=()
  175. fi
  176. "$TERMSH" create-shell-favorite "${RUN_OPTS[@]}" "$NAME (root)" "$RUN 0:0" > /dev/null
  177. "$TERMSH" create-shell-favorite "${RUN_OPTS[@]}" "$NAME" "$RUN" > /dev/null
  178. if typeset -f finally >/dev/null 2>&1 ; then finally ; fi
  179.  
  180. fi
  181.  
  182. echo 'Done.'
  183.  
  184.  
  185. # There is no uname on old Androids.
  186. ARCH="$(validate_arch "$(uname -m 2>/dev/null)" || ( aa=($MY_DEVICE_ABIS) ; to_uname_arch "${aa[0]}" ))"
  187.  
  188. VARIANT=''
  189. if [ -n "$MY_ANDROID_SDK" -a "$MY_ANDROID_SDK" -lt 21 ]
  190. then
  191. VARIANT='-pre5'
  192. fi
  193.  
  194. to_minitar_arch() {
  195. case "$1" in
  196. armv7a) echo armeabi-v7a ;;
  197. aarch64) echo arm64-v8a ;;
  198. i686) echo x86 ;;
  199. amd64) echo x86_64 ;;
  200. *) echo "$1" ;;
  201. esac
  202. }
  203.  
  204. to_lco_arch() {
  205. case "$1" in
  206. armv7a) echo armhf ;;
  207. aarch64) echo arm64 ;;
  208. i686) echo i386 ;;
  209. x86_64) echo amd64 ;;
  210. *) echo "$1" ;;
  211. esac
  212. }
  213.  
  214. to_lco_link() {
  215. echo "http://192.168.43.239:8080/rootfs.tar.xz"
  216. }
  217.  
  218. echo
  219. echo "Arch: $ARCH"
  220. echo "Variant: $VARIANT"
  221. echo "Root FS: $DISTRO $RELEASE"
  222. echo
  223.  
  224. if [ -z "$ROOTFS_URL" ] ; then
  225. ROOTFS_URL="http://192.168.43.239:8080/rootfs.tar.xz"
  226. fi
  227.  
  228. echo "Source: $ROOTFS_URL"
  229. echo
  230.  
  231. cd "$DATA_DIR"
  232. (
  233.  
  234. OO="$([ -t 2 ] && echo --progress)"
  235.  
  236.  
  237. # = Essentials =
  238. if E_MINITAR="$("$TERMSH" plugin "$ESSENTIALS" minitar)" 2>/dev/null
  239. then MINITAR="$E_MINITAR"
  240. # ==============
  241. else
  242.  
  243. echo 'Getting minitar...'
  244.  
  245. "$TERMSH" cat $OO \
  246. "http://192.168.43.239:8080/minitar" \
  247. > "$MINITAR"
  248. chmod 755 "$MINITAR"
  249.  
  250. fi
  251.  
  252.  
  253. # = Essentials =
  254. if E_PROOT="$("$TERMSH" plugin "$ESSENTIALS" proot)" 2>/dev/null
  255. then
  256. PROOT="\$(\"\$TERMSH\" plugin '$ESSENTIALS' proot)"
  257. PROOT_USERLAND="\$(\"\$TERMSH\" plugin '$ESSENTIALS' proot-userland)" || true
  258. # ==============
  259. else
  260.  
  261. echo 'Getting PRoot...'
  262.  
  263. "$TERMSH" cat $OO \
  264. "http://192.168.43.239:8080/proot-android-armv7a-pre5.tar.gz" \
  265. | "$MINITAR"
  266.  
  267. fi
  268.  
  269.  
  270. # = Test =
  271. [ -n "$MY_ANDROID_SDK" -a "$MY_ANDROID_SDK" -ge 29 \
  272. -a -n "$APP_TARGET_SDK" -a "$APP_TARGET_SDK" -ge 29 ] \
  273. && { eval "$PROOT" --help > /dev/null 2>&1 || \
  274. exit_with "$(
  275. echo 'Current PRoot version does not start.'
  276. echo "Your Android version is 10 (API 29) or higher and this Another Term version targets API $APP_TARGET_SDK."
  277. echo 'See https://green-green-avk.github.io/AnotherTerm-docs/local-shell-w-x.html#main_content'
  278. )" ; } || true
  279. # ========
  280.  
  281.  
  282. mkdir -p "$ROOTFS_DIR/root"
  283. mkdir -p "$ROOTFS_DIR/tmp"
  284. cd "$ROOTFS_DIR/root"
  285.  
  286.  
  287. echo 'Getting Linux root FS...'
  288.  
  289. "$TERMSH" cat $OO "$ROOTFS_URL" | "$MINITAR" || echo 'Possibly URL was changed: recheck on the site.' >&2
  290.  
  291.  
  292. if [ -z "$NI" ] ; then
  293. echo
  294. echo -e '\e[1m/etc/passwd:\e[0m'
  295. echo '\e[1m=======\e[0m'
  296. cat etc/passwd
  297. echo '\e[1m=======\e[0m'
  298. prompt 'Regular user name' "$REG_USER" REG_USER
  299. prompt 'Preferred shell' "$FAV_SHELL" FAV_SHELL
  300. echo
  301. fi
  302.  
  303.  
  304. echo 'Setting up run script...'
  305.  
  306. mkdir -p etc/proot
  307. cat << EOF > etc/proot/run.cfg
  308. # Main configuration
  309.  
  310. # Regular user name
  311. USER=${REG_USER@Q}
  312.  
  313. # Preferred shell (fallback: /bin/sh)
  314. SHELL=${FAV_SHELL@Q}
  315.  
  316. # =======
  317.  
  318. PROOT=$PROOT
  319. PROOT_USERLAND=$PROOT_USERLAND
  320.  
  321. # Mostly for Android < 5 now. Feel free to adjust.
  322. # Not recommended to set it >= '4.8.0' for kernels < '4.8.0'
  323. # becouse of a random number generation API change at this point
  324. # as it could break libopenssl random number generation routine.
  325. _KERNEL_VERSION="$(uname -r 2>/dev/null || echo 0)"
  326. if [ "\${_KERNEL_VERSION%%.*}" -lt 4 ] ; then
  327. PROOT_OPT_ARGS+=('-k' '4.0.0')
  328. fi
  329.  
  330. # Android >= 9 can have a read restictiction
  331. # on '/proc/version'.
  332. cat /proc/version >/dev/null 2>&1 || {
  333. _PROC_VERSION="\$CFG_DIR/proc.version.cfg"
  334. { uname -a 2>/dev/null || echo 'Linux' ; } > "\$_PROC_VERSION"
  335. PROOT_OPT_ARGS+=('-b' "\$_PROC_VERSION:/proc/version")
  336. }
  337.  
  338. # Application data shared directory.
  339. PROOT_OPT_ARGS+=('-b' "\$SHARED_DATA_DIR:/mnt/shared")
  340.  
  341. # Uncomment to manipulate Android application own private data directory.
  342. #PROOT_OPT_ARGS+=('-b' '/data')
  343.  
  344. # =======
  345. EOF
  346. cat << EOF > etc/proot/run.rc
  347. # Right before proot starting
  348.  
  349. if ! is_root
  350. then
  351. PROOT_OPT_ARGS+=('--mute-setxid') # 'make' should be happy now...
  352. fi
  353. EOF
  354. "$TERMSH" cat $OO \
  355. 'http://192.168.43.239:8080/run-tpl' \
  356. > etc/proot/run
  357. chmod 755 etc/proot/run
  358. rm -r ../run 2>/dev/null || true # Jelly Bean has no `-f' option (API 16 at least).
  359. ln -s root/etc/proot/run ../run # KitKat can only `ln -s'.
  360.  
  361.  
  362. echo 'Configuring...'
  363.  
  364. cat << EOF > bin/termsh
  365. #!/bin/sh
  366.  
  367. unset LD_PRELOAD
  368. unset LD_LIBRARY_PATH
  369. /bin/_termsh "\$@"
  370. EOF
  371.  
  372. chmod 700 bin/termsh
  373.  
  374. cat << EOF > etc/resolv.conf
  375. nameserver 8.8.8.8
  376. nameserver 8.8.4.4
  377. EOF
  378.  
  379. cat << EOF > etc/profile.d/locale.sh
  380. if [ -f /etc/default/locale ]
  381. then
  382. . /etc/default/locale
  383. export LANG
  384. fi
  385. EOF
  386. cat << EOF > etc/profile.d/ps.sh
  387. PS1='\[\e[32m\]\u\[\e[33m\]@\[\e[32m\]\h\[\e[33m\]:\[\e[32m\]\w\[\e[33m\]\\$\[\e[0m\] '
  388. PS2='\[\e[33m\]>\[\e[0m\] '
  389. EOF
  390.  
  391. # We have no adduser or useradd here...
  392. cp -a etc/skel home/$REG_USER 2>/dev/null || mkdir -p home/$REG_USER
  393. echo \
  394. "$REG_USER:x:$USER_ID:$USER_ID:guest:/home/$REG_USER:$FAV_SHELL" \
  395. >> etc/passwd
  396. )
  397.  
  398.  
  399. echo -e '\nDone!\n'
  400.  
Advertisement
RAW Paste Data Copied
Advertisement