October 10

Android: Linuxboot.sh – Script for booting Linux in Android

#!/data/data/com.galoula.LinuxInstall/bin/busybox sh
# Many variables
export kit=/mnt/sdcard/linux/ubuntu.img
export bin=/system/bin
export mnt=/data/data/com.galoula.LinuxInstall/mnt
export FS=ext2
export PATH=/usr/bin:/usr/sbin:/bin:$PATH
export TERM=linux
export HOME=/root
export USER=root
export LOGNAME=root
export UID=0
export SHELL=bash
export BINDS=”/dev /data /proc /sys /system /cache /devlog /vendor /acct /sdcard /config /app-cache ”
export busybox=”/data/data/com.galoula.LinuxInstall/bin/busybox”

testmounted()
{
$busybox mount | $busybox grep $mnt > /dev/null || false
}

testuseloop()
{
$busybox losetup | $busybox grep “$kit” || false
}

MountBinds()
{
for i in $BINDS
do
if $busybox [ ! -d $mnt$i ]
then
mkdir $mnt$i
fi
$busybox mount -o bind $i $mnt$i
done
}

UnMountBinds()
{
for i in $BINDS
do
if $busybox [ ! -d $i ]
then
mkdir $i
fi
$busybox umount $mnt$i
done
}

testLoop()
{
if $busybox [ ! -e /dev/block/loop6$1 ]
then
$busybox mknod /dev/block/loop6$1 b 7 $1
$busybox losetup /dev/block/loop6$1 $kit || false
else
$busybox losetup /dev/block/loop6$1 $kit || false
fi
#    if [ ! -e /dev/loop$1 ]
#    then
#        mknod /dev/loop$1 b 7 $1
#        $busybox losetup /dev/loop$1 $kit || false
#    else
#        $busybox losetup /dev/loop$1 $kit || false
#    fi
}

testmounted
mounted=$?
if $busybox [ $mounted -eq 0 ]
then
echo Linux LOOP FILE ALREADY MOUNTED !
echo Starting Linux without mount anything.
echo To exit Linux, exit all thread and
echo exit the FIRST bootdeb at LAST !!!
fi

testuseloop
useloop=$?
if $busybox [ $useloop -eq 0 ]
then
echo $busybox LOOP FILE IN USE MOUNTED !
if $busybox [ ! $mounted -eq 0 ]
then
exit 3
fi
fi

if $busybox [ $useloop -eq 1 -a $useloop -eq 1 ]
then
export result=1
export loop=-1
while $busybox [ $result -eq 1 ]
do
# How many devices to look through to find a free one.  The higher the number the slower the boot.
if $busybox [ $loop -eq 32 ]
then
echo “No free loop device found !”
exit 1
fi
loop=$((loop+1))
testLoop $loop
result=$?
done
echo “I use /dev/block/loop6$loop”
$busybox mount -t $FS /dev/block/loop6$loop $mnt
fi

if $busybox [ $mounted -eq 1 ]
then
# This is for my tests for direct hardware acces.
$busybox mount -o bind /dev $mnt/dev
$busybox mount -o bind /sys $mnt/sys
$busybox mount -o bind /proc $mnt/proc

# Mount Android on Debian
MountBinds

# FIX the “stdin: is not a tty” error in direct hadware case.
$busybox mount -t devpts devpts $mnt/dev/pts

# For the network.
#sysctl -w net.ipv4.ip_forward=1
echo 1 > /proc/sys/net/ipv4/ip_forward

# For a nice output for the df command !
echo -n “” >$mnt/etc/mtab
S1=”AndroidLoopType$loop $mnt ext2 rw,errors=continue 0 0″
$busybox cat /proc/mounts | while read line
do
S2=”$line”
if $busybox [ “$S1” = “$S2” ]; then
break;
else
echo “$line”>>$mnt/etc/mtab
fi
done
# Cleanup tmp folder.
$busybox rm -rf $mnt/tmp
$busybox mkdir $mnt/tmp
# Enter Linux 🙂
fi
$busybox chroot $mnt /usr/sbin/init.sh
if $busybox [ $mounted -eq 1 ]
then
# while you type “exit” the script continue here : I unmount all.
sync
UnMountBinds
$busybox umount $mnt/dev/pts
$busybox umount $mnt/dev
$busybox umount $mnt/sys
$busybox umount $mnt/proc
$busybox umount $mnt/dev/pts 2> /dev/null
$busybox umount $mnt/dev 2> /dev/null
sync
$busybox umount $mnt && sleep 1
$busybox losetup -d /dev/block/loop6$loop
# If you you have propely exit all Debian process, all are unmounted succesfully
else
echo “Do you want try force umount chroot ?”
echo “Press 1 for YES.”
read REPLY
if $busybox [ 1$REPLY -eq 11 ]
then
UnMountBinds
$busybox umount $mnt/dev/pts
$busybox umount $mnt/dev
$busybox umount $mnt/sys
$busybox umount $mnt/proc
$busybox umount $mnt/dev/pts 2> /dev/null
$busybox umount $mnt/dev 2> /dev/null
sync
$busybox umount $mnt && sleep 1
$busybox losetup -d /dev/block/loop6$loop
fi
fi


Copyright 2021. All rights reserved.

Posted October 10, 2011 by Timothy Conrad in category "Android

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B