Skip to content

Instantly share code, notes, and snippets.

@mikroskeem
Last active August 30, 2018 16:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikroskeem/635f91419e4aef0503f09fd0adf31bd1 to your computer and use it in GitHub Desktop.
Save mikroskeem/635f91419e4aef0503f09fd0adf31bd1 to your computer and use it in GitHub Desktop.
A script to get Arch Linux ARM rootfs running in proot on Android (Termux)
/*
* clang -static -O2 get_shell.c get_shell
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
int main(int argc, char *argv[]) {
if(argc < 2) {
printf("Usage: prog <username or uid:gid pair>\n");
return 1;
}
// uid & gid
int uid = -1,
gid = -1;
char* username = argv[1];
size_t size = strlen(username);
// Open passd file and struct
struct passwd* passwd;
FILE* passwd_file = fopen("/data/data/com.termux/files/home/.alarm/etc/passwd", "r");
// Check for ':'
// TODO: this is hackish
char* separator = strstr(username, ":") == NULL ? NULL : strsep(&username, ":");
if(separator != NULL) {
uid = atoi(separator);
gid = atoi(username);
}
// Iterate over password entries
while((passwd = fgetpwent(passwd_file)) != NULL) {
if(((uid != -1 && gid != -1) && (uid == passwd->pw_uid && gid == passwd->pw_gid)) || strncmp(passwd->pw_name, username, size) == 0) {
printf("%s\n", passwd->pw_shell);
return 0;
}
}
return 1;
}
#!/bin/bash -e
if [ -z "${ANDROID_ROOT}" ]; then
echo "Don't :)"
exit 1
fi
# This breaks proot, see https://wiki.termux.com/wiki/PRoot
unset LD_PRELOAD
# Arch Linux ARM unpacked root location
ROOT_LOCATION="$(realpath ~/.alarm)"
# Note: user `alarm` is 1000:1000, so use `WHO=1000:1000 launch-arch.sh`
WHO=${WHO:-0:0}
# Program to launch
PROG=${@:-$(get_shell ${WHO}) -l}
# Launch proot
proot \
--link2symlink \
--kill-on-exit \
-i "${WHO}" \
-r "${ROOT_LOCATION}" \
-b /dev \
-b /proc \
-b /sys \
-b /sdcard \
-b /data/data/com.termux/files \
/usr/bin/env -i -C "${HOME}" \
PATH=/usr/bin:/usr/local/bin \
TERM="${TERM}" \
HOME="${HOME}" \
${PROG}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment