首页 > tips, TroubleShooting > term-init.sh:使得android4.1.2的linux支持init.d

term-init.sh:使得android4.1.2的linux支持init.d

2013年9月14日 发表评论 阅读评论

  手头有一个三星的sch-i879(cdma2000+gsm双卡双待)手机。官方的ROM(4.1.2)有一堆的不如意,尽管已经root了,而且也干掉了一大堆的无用的被植入的应用,而且还把google的支持加进去了,但还是有一个非常大的硬伤:android 4.1.2的virtual SD card,这是一种虚拟的sdcard技术,实际上是用/data/media目录做了个fuse文件系统,然后挂载成/storage/sdcard0,也就是/sdcard的真实路径。这个技术在系统没有插入tf卡的时候是比较好用的,因为好多应用都必须有sdcard才能跑起来,但对于我插的有32G class10的tf卡来说,这个功能就相当鸡肋了。
  原因有二:
1,本身机器就内存(指ROM)不够,sd卡还从用户分区(/data)里刨去一块儿,用户可用内存(ROM)更小了。
2,机器内存(ROM)小,用户分区小就导致了这种方式下的/sdcard空间不可能很大的,这样就导致了像地图导航软件、词典软件等等需要把大量数据放到/sdcard上的应用来讲是不可接受的
解决方案:
  大家用过Linux的知道,在/etc/init.d目录下可以放好些脚本,用来在启动机器时做一些定制的工作。android也是linux,故而也应该能支持,但目前现状就是很多官方ROM都没打开这个支持,不幸的是我的i879也在其列。于是我就在网上找,终于在xda上找到一个脚本:term-init.sh,可以用来使android4.1.2支持init.d。

附上脚本内容:

#!/system/bin/sh
#Script to enable init.d by Ryuinferno @ XDA

error_msg(){
echo "You do not need this mod..."
sleep 1
echo "If you are reapplying, please delete these files if present:"
echo "/system/bin/sysinit"
sleep 1
echo "/system/etc/install-recovery.sh"
sleep 1
echo "/system/etc/install-recovery-2.sh"
sleep 1
echo "And run again..."
sleep 1
echo "If init.d is still not working, read the FAQ part in my thread..."
sleep 1
echo "Aborting..."
mount -o remount,ro -t auto /system
echo ""
echo "Ryuinferno @ XDA"
exit 1
}

echo "Init.d Enabler by Ryuinferno @ XDA"
echo ""
sleep 1

id=`id`; 
id=`echo ${id#*=}`; 
id=`echo ${id%%\(*}`; 
id=`echo ${id%% *}`
if [ "$id" != "0" ] && [ "$id" != "root" ]; then
	echo "Script NOT running as root!"
	sleep 1
	echo "Superuser access not granted!"
	sleep 1
	echo "Please type 'su' first before running this script..."
	exit 1
else
	echo "Hello Supaa User! :P"
	echo ""
	sleep 1
fi

if [ ! "'which busybox'" ]; then
	echo "busybox NOT INSTALLED!"
	sleep 1
	echo "Please install busybox first!"
	exit 1
else
	echo "busybox found!"
	sleep 1
fi

bbb=0

if [ ! "`which grep`" ]; then 
	bbb=1
	echo "grep applet NOT FOUND!"
	sleep 1
	else 
	echo "Awesome! grep found! :D"
	sleep 1
fi

if [ ! "`which run-parts`" ]; then 
	bbb=1
	echo "run-parts applet NOT FOUND!"
	sleep 1
	else
	echo "Good! run-parts found! :)"
	echo ""
	sleep 1
fi

if [ $bbb -eq 1 ] ; then
	echo ""
	echo "Required applets are NOT FOUND!"
	echo ""
	sleep 1
	echo "Please reinstall busybox!"
	exit 1
fi

echo "Great! Let's proceed..."
echo ""
sleep 1
echo "Press enter to continue..."
read enterKey

clear
sleep 1
echo "Mounting system as rewritable..."
mount -o remount,rw -t auto /system

sleep 1
echo ""
echo "Checking for the presence of sysinit in /system/bin..."
sleep 1
if [ -e /system/bin/sysinit ]; then
	echo "sysinit found..."
	if [ -z "`cat /system/bin/sysinit | grep "init.d"`" ]; then
		echo "Adding lines to sysinit..."
		echo "" >> /system/bin/sysinit
		echo "# init.d support" >> /system/bin/sysinit
		echo "" >> /system/bin/sysinit
		echo "export PATH=/sbin:/system/sbin:/system/bin:/system/xbin" >> /system/bin/sysinit
		echo "run-parts /system/etc/init.d" >> /system/bin/sysinit 
		echo "" >> /system/bin/sysinit
	else
		echo ""
		echo "Your sysinit should already be running the scripts in init.d folder at boot..."
		error_msg
	fi
else
	echo "sysinit not found, creating file..."
	echo "#!/system/bin/sh" > /system/bin/sysinit
	echo "# init.d support" >> /system/bin/sysinit
	echo "" >> /system/bin/sysinit
	echo "export PATH=/sbin:/system/sbin:/system/bin:/system/xbin" >> /system/bin/sysinit
	echo "run-parts /system/etc/init.d" >> /system/bin/sysinit 
	echo "" >> /system/bin/sysinit
fi

sleep 1
echo "Setting correct permissions and ownership for sysinit..."
chmod 755 /system/bin/sysinit
chown 0.2000 /system/bin/sysinit

sleep 1
echo ""
echo "Checking for the presence of install-recovery.sh..."
sleep 1
if [ -f /system/etc/install-recovery.sh ] && [ -z "`cat /system/etc/install-recovery.sh | grep "daemon"`" ]; then
	if [ ! -z "`cat /system/etc/install-recovery.sh | grep "init.d"`" ];then
		echo "Your install-recovery.sh seems to be already modified for init.d..."
		error_msg
	fi
	echo "install-recovery.sh found, renaming it as install-recovery-2.sh..."
	mv /system/etc/install-recovery.sh /system/etc/install-recovery-2.sh
	echo "Recreating install-recovery.sh..."
	echo "#!/system/bin/sh" > /system/etc/install-recovery.sh
	echo "# init.d support" >> /system/etc/install-recovery.sh
	echo "" >> /system/etc/install-recovery.sh
	echo "/system/bin/sysinit" >> /system/etc/install-recovery.sh
	echo "" >> /system/etc/install-recovery.sh
	echo "# excecuting extra commands" >> /system/etc/install-recovery.sh
	echo "/system/etc/install-recovery-2.sh" >> /system/etc/install-recovery.sh
	echo "" >> /system/etc/install-recovery.sh
elif [ -f /system/etc/install-recovery.sh ] && [ ! -z "`cat /system/etc/install-recovery.sh | grep "daemon"`" ]; then
	if [ -f /system/etc/install-recovery-2.sh ] && [ ! -z "`cat /system/etc/install-recovery-2.sh | grep "init.d"`" ];then
		echo "Your install-recovery-2.sh seems to be already modified for init.d..."
		error_msg
	fi
	echo "install-recovery.sh is used for superuser, using install-recovery-2.sh instead..."
	if [ -f /system/etc/install-recovery-2.sh ]; then
		echo "" >> /system/etc/install-recovery-2.sh
		echo "# init.d support" >> /system/etc/install-recovery-2.sh
		echo "/system/bin/sysinit" >> /system/etc/install-recovery-2.sh
		echo "" >> /system/etc/install-recovery-2.sh
	else
		echo "#!/system/bin/sh" > /system/etc/install-recovery-2.sh
		echo "# init.d support" >> /system/etc/install-recovery-2.sh
		echo "" >> /system/etc/install-recovery-2.sh
		echo "/system/bin/sysinit" >> /system/etc/install-recovery-2.sh
		echo "" >> /system/etc/install-recovery-2.sh
	fi
	if [ -z "`cat /system/etc/install-recovery.sh | grep "install-recovery-2.sh"`" ]; then
		echo "" >> /system/etc/install-recovery.sh
		echo "# extra commands" >> /system/etc/install-recovery.sh
		echo "/system/etc/install-recovery-2.sh" >> /system/etc/install-recovery.sh
		echo "" >> /system/etc/install-recovery.sh
	fi
else
	echo "install-recovery.sh not found, creating it..."
	echo "#!/system/bin/sh" > /system/etc/install-recovery.sh
	echo "# init.d support" >> /system/etc/install-recovery.sh
	echo "" >> /system/etc/install-recovery.sh
	echo "/system/bin/sysinit" >> /system/etc/install-recovery.sh
	echo "" >> /system/etc/install-recovery.sh
fi

sleep 1
echo "Setting the correct permissions and ownership for install-recovery.sh..."
echo "Also for install-recovery-2.sh if it exists..."
chmod 755 /system/etc/install-recovery.sh
chown 0.0 /system/etc/install-recovery.sh
if [ -f /system/etc/install-recovery-2.sh ]; then
	chmod 755 /system/etc/install-recovery-2.sh
	chown 0.0 /system/etc/install-recovery-2.sh
fi

sleep 1
echo ""
echo "Checking for the presence of the init.d folder..."
sleep 1
if [ -d /system/etc/init.d ]; then
	echo "init.d folder found..."
else 
	echo "init.d folder not found, creating the folder..."
	mkdir /system/etc/init.d
fi

sleep 1
echo ""
echo "Creating basic init.d scripts..."
echo "#!/system/bin/sh" > /system/etc/init.d/08setperm
echo "#set correct permissions to /system/etc/init.d folder" >> /system/etc/init.d/08setperm
echo "" >> /system/etc/init.d/08setperm
echo "mount -o remount,rw -t auto /system" >> /system/etc/init.d/08setperm
echo "chmod -R 777 /system/etc/init.d" >> /system/etc/init.d/08setperm
echo "mount -o remount,ro -t auto /system" >> /system/etc/init.d/08setperm
echo "" >> /system/etc/init.d/08setperm

echo "#!/system/bin/sh" > /system/etc/init.d/00test
echo "#init.d test" >> /system/etc/init.d/00test
echo "" >> /system/etc/init.d/00test
echo "if [ -f /data/Test.log ]; then" >> /system/etc/init.d/00test
echo "rm /data/Test.log" >> /system/etc/init.d/00test
echo "fi" >> /system/etc/init.d/00test
echo "" >> /system/etc/init.d/00test
echo 'echo "Init.d is working !!!" >> /data/Test.log' >> /system/etc/init.d/00test
echo 'echo "excecuted on $(date +"%d-%m-%Y %r" )" >> /data/Test.log' >> /system/etc/init.d/00test
echo "" >> /system/etc/init.d/00test

sleep 1
echo "Setting correct permissions and ownership for init.d folder and scipts..."
chmod 777 /system/etc/init.d
chmod 777 /system/etc/init.d/08setperm
chmod 777 /system/etc/init.d/00test
chown 0.0 /system/etc/init.d
chown 0.0 /system/etc/init.d/08setperm
chown 0.0 /system/etc/init.d/00test

sleep 1
echo ""
echo "Mounting system as read-only..."
mount -o remount,ro -t auto /system
sleep 1
echo ""
echo "Done!!!"
sleep 1
echo "Please reboot at least twice before checking /data..."
sleep 1
echo "If init.d is working, you will see a Test.log in /data..."
sleep 1
echo ""
echo "Enjoy!!! =)"
echo "Ryuinferno @ XDA 2013"
exit

使用方法:
1,将手机root(越狱);
2,安装busybox(支持run-parts)、终端模拟器;
3,将脚本拷贝到/sdcard上,并赋予执行权限;
4,在终端模拟器上su成root,然后再执行这个脚本,按照提示执行即可;
5,重启后发现/data下有个文件叫Test.log,即成功!

后续工作:
  系统支持init.d之后呢?我们这么干是为了什么呢?呵呵,我想是在init.d目录下写个脚本,将/storage/sdcard0和/storage/extSdcard用bind方式挂载到一起,在这之前先删掉/data/media/目录下的东西,这样以来就算是脏脏的解决了现有的问题了。顺手贴上我的脚本(/etc/init.d/extsd2inter):

#!/system/bin/sh
#extsd2inter is a modification that allows to switch internal sd to ex

if cat /proc/mounts | grep -q "/storage/sdcard0"; then
	if lsof | grep -q "/storage/sdcard0"; then
		fuser -km /storage/sdcard0
	fi
	umount /storage/sdcard0
	if cat /proc/mounts | grep -q "/storage/sdcard0"; then
		# umount failed
	else
		# umount success
	fi
fi
until cat /proc/mounts | grep -q "/storage/extSdCard"; do
	sleep 1
done
if cat /proc/mounts | grep -q "/storage/sdcard0"; then
	if lsof | grep -q "/storage/sdcard0"; then
		fuser -km /storage/sdcard0
	fi
	umount /storage/sdcard0
fi
mount -o bind /storage/extSdCard/ /storage/sdcard0
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.