Skip to content

Instantly share code, notes, and snippets.

@chaoskagami
Created July 7, 2016 20:20
Show Gist options
  • Save chaoskagami/2f21cf417a36b7f560148196fdd14024 to your computer and use it in GitHub Desktop.
Save chaoskagami/2f21cf417a36b7f560148196fdd14024 to your computer and use it in GitHub Desktop.
How not to use Android/CyanogenMod 11
#!/system/bin/sh
function slog() {
log -p i -t hax "$1"
echo "$1" >> /tmp/gentoo.log
}
while [ ! -e /dev/block/mmcblk1p1 ]; do
slog "No sd block device. Waiting 30s..."
sleep 30
done
#####################################
# Script to support attaching #
# an SD card with a linux distro on #
# it #
#####################################
BB=/system/xbin/busybox
MNT=/system/bin/mount
# slog "Setting permissive"
# setenforce 0
slog "RWing root"
# Remount root rw. This only stays in memory, anyways. Not a big deal.
$MNT -o remount,rw /
#Fixed as of 5.1.1 tmp is tmpfs already
#slog "Reattach /tmp as tmpfs"
# Save tmp for a second.
#$BB mv /tmp /tmpold
#$BB mkdir /tmp
#$BB chmod 777 /tmp
# Attach tmp as tmpfs, because its not wtf
#$MNT -t tmpfs tmpfs /tmp
# Restore what's in there (whatever it is)
#$BB mv /tmpold/* /tmp/
## Delete old tmp
#$BB rm -rf /tmpold
slog "Attach devpts"
# devpts
$MNT -t devpts none /dev/pts
slog "/dev/fd symlink"
# Fix for...issues. Building python needs this, for example.
$BB ln -s /proc/self/fd /dev/fd
# Attach of gentoo and patch root in memory to hybridize.
if [ -e /dev/block/mmcblk1p2 ]; then
slog "Linux payload, becoming hybrid bastard OS"
# script is a little different since CM isn't stupid
$BB mkdir -p /data/gentoo
$BB chmod 755 /data/gentoo
# Mount gentoo
$MNT -o rw -t ext4 /dev/block/mmcblk1p2 /data/gentoo
$BB rm -rf /bin
$BB mkdir -p /bin
$MNT -o bind /data/gentoo/bin /bin
# Relink things.
$BB rm /etc
$BB mkdir /etc
$MNT -o bind /data/gentoo/etc /etc
$BB mkdir /lib
$MNT -o bind /data/gentoo/lib /lib
$BB mkdir /opt
$MNT -o bind /data/gentoo/opt /opt
# Relink /root to /data/gentoo/root.
$BB rm -rf /root
$BB mkdir /root
$MNT -o bind /data/gentoo/root /root
$BB mkdir /home
$MNT -o bind /data/gentoo/home /home
$BB mkdir /run
$MNT -o bind /data/gentoo/run /run
# Sbin needs to be fixed.
$BB rm -rf /sbin/*
$MNT -o bind /data/gentoo/sbin /sbin
# Last.
$BB mkdir /usr
$MNT -o bind /data/gentoo/usr /usr
$BB mkdir /var
$MNT -o bind /data/gentoo/var /var
# shm doesnt exist. fix.
$BB mkdir /dev/shm
$BB mount -t tmpfs tmpfs /dev/shm
$BB chmod 1777 /dev/shm
# Bind stuff into chroot
$BB mount --rbind /dev /data/gentoo/dev
$BB mount --rbind /sys /data/gentoo/sys
$BB mount -t proc none /data/gentoo/proc
if [ -f /etc/linux-init ]; then
chmod 755 /etc/linux-init
cd /
/etc/linux-init &
fi
else
slog "No linux found. oh well"
fi
if [ -e /dev/block/mmcblk1p3 ]; then
slog "attach swap"
$BB swapon /dev/block/mmcblk1p3
fi
@chaoskagami
Copy link
Author

chaoskagami commented Feb 17, 2019

I'll just leave a note here that this requires a few things (from memory):

  • The contents of /etc have to be duplicated to the chroot to avoid issues.
  • The contents of the ramdisk also need to be transferred to avoid issues.
  • This way of doing things requires selinux to be disabled (not permissive, disabled) which means it won't work after kitkat. It especially will not work with the new way that android keeps system partitions formatted.

The same can probably be accomplished using overlayfs in a more modern kernel with much fewer hacks. In many ways however, this has been completely obsoleted by Termux, though the implementation of clang and autotools on Termux leave a lot to be desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment