Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,808,901 Members 54,615 Now Online
XDA Developers Android and Mobile Development Forum

[MOD][SCRIPTS] Boot Script Utilities

Tip us?
 
DarkThanos
Old
(Last edited by DarkThanos; 25th August 2014 at 06:09 PM.)
#1  
DarkThanos's Avatar
Senior Member - OP
Thanks Meter 41
Posts: 210
Join Date: Mar 2011
Location: Chania

 
DONATE TO ME
Database [MOD][SCRIPTS] Boot Script Utilities


About this thread
Quote:
In this thread i will post my work on a init.d like support for stock rom which i call "Boot Script Utilities".
This work was developed for personal use and for my personal purposes.
If you do not like to use it please do not.
Just to be clear
I am not responsible for whatever happens to your phone.

Some Info
Quote:
What is init.d?
Init.d is a folder located in "/system/etc/init.d/", but not all roms have his folder (stock rom don't have the folder).
What is special with this folder is that any script inside this folder will run on phone boot.

Why to run scripts on boot?
On boot many system variables are initiated before any application run, so you can make automatic tweaks on every boot.
ex. Swap external storage to internal.

Boot Script Utilities on stock Samsung Galaxy Core
This was developed for personal use. This is not the original init.d support.
I developed a similar support in order to implement some more features.
This do not means that this is a better way to roon scripts on boot.

Quote:
What do i need?
A rooted Samsung Galaxy Core (I used I8260, for how to root search the forum)
A good root explorer (well Root Explorer worked)
A recovery with backup/restore support (to backup your rom in case you soft brick your phone)

What we will do?
We will edit a system file to run a script i wrote. This script will run all scripts (or not all read more below) inside /system/etc/init.d/ folder.

Step 0 - Backup your rom
Boot into your recovery and make a backup.

Step 1 - init.d folder
Create a directory on "/system/etc/" and name it "init.d".
So now directory "/system/etc/init.d/" exist.

Step 2 - Boot Script Utilities main script
Copy "init.bsu.boot.sh" file (link on the end of the post) in "/system/etc/".
So now file "/system/etc/init.bsu.boot.sh" exist.

Step 3 - Edit system file, run Boot Script Utilities
The system file we will edit is "/system/etc/init.qcom.post_boot.sh"
Insert this code on the bottom of the script
Code:
# Run Boot Script Utilities
# For galaxy core stock rom
# by DarkThanos
/system/bin/sh /system/etc/init.bsu.boot.sh > /data/tmp/init.bsu.log 2>&1 &
Step 4 - Ready
Insert your scripts inside "/system/etc/init.d/" and whey will run on boot.

Boot Script Utilities features
Quote:

Disable all scripts
If file "/system/etc/init.d/disabled" exist no script will run.

Disable scripts
Any script named like "*.disabled" or "*.disabled.sh" will not run.
ex. "/system/etc/init.d/testscript.disabled.sh" will not run.

Run scripts asynchronous
Any script named like "*.async" or "*.async.sh" will run asynchronous.
ex. "/system/etc/init.d/testscript.async.sh" will run asynchronous.

Scripts run order
Scripts run in name order.

Log on every boot
All scripts results-errors output (stdout/stderr) will be printed on "/data/tmp/init.bsu.log"
[Change log]
 

Quote:
  • 1.0 Version
    • Script Public for first time.
    • init.d like support

[Dev Doc]
 

Quote:
It does not really matters from where the script is called.

The script first checks if boot is completed, if not it sleeps for 2 seconds.
(so you need to call the script after boot, or before but asynchronous)
Code:
# Get boot complete state
	isBootCompleted=`getprop dev.bootcomplete`
# Wait For boot complete event
# Probably useless unless script called from other file
	while [ "$isBootCompleted" -ne '1' ]
	do
		# Wait and check again
		sleep 2
		# Check if boot completed
		isBootCompleted=`getprop dev.bootcomplete`
	done
The next step is to check if "/system/etc/init.d/disabled" file exist.
If it do not exist, it will run all not disabled scripts inside "/system/etc/init.d/".
It will check every file inside and analyze its name so it knows if the script is disabled or asynchronous.
Code:
# Create a init.d Support
# Implement /system/etc/init.d/ folder
	# Check if init.d is disabled
	# If /system/etc/init.d/disabled file exist, then init.d is disabled
	if [[ ! -f /system/etc/init.d/disabled ]]
	then
		echo "[$manager_name] Initiating init.d scripts"
		# Run every script on init.d folder
		for scriptfile in /system/etc/init.d/*
		do
			if test -f "$scriptfile"
			then
				# Get script name
				scriptname=$(basename $scriptfile)
				
				# Check if script is disabled
				# ex1 : 'testscript.sh.disabled' is disabled
				# ex1 : 'testscript.disabled.sh' is disabled 
				echo "$scriptname" | grep -q -E '\.(disabled|disable\.sh)$'
				if [ $? -eq 0 ]
				then
					echo "[$manager_name] Script $scriptname is disabled"
				else
					# check if script is asynchronous
					# ex1 : 'testscript.sh.async' is asynchronous
					# ex2 : 'testscript.async.sh' is asynchronous
					echo "$scriptname" | grep -q -E '\.(async|async\.sh)$'
					if [ $? -eq 0 ]
					then
						echo "[$manager_name] Run Script $scriptname asynchronous"
						/system/bin/sh "$scriptfile" | script_log &
					else
						echo "[$manager_name] Run Script $scriptname"
						/system/bin/sh "$scriptfile" | script_log
					fi
				fi
			fi
		done
	else
		echo '[$manager_name] init.d is disabled'
	fi
That's all.
Please suggest any fixes or any improvements by messaging me.
Thanks for your time.

Attached Files
File Type: zip init.bsu.boot v1.0.zip - [Click for QR Code] (1,009 Bytes, 5 views)
SOME PEOPLE ONLY SEE WORDS (⊙.⊙(☉_☉)⊙.⊙) BUT OTHER PEOPLE DO NOT ONLY SEE, THEY READ TOO
READING IS A GIFT NOWADAYS... ARE YOU GIFTED ?
The Following 2 Users Say Thank You to DarkThanos For This Useful Post: [ Click to Expand ]
 
DarkThanos
Old
(Last edited by DarkThanos; 24th August 2014 at 10:58 PM.)
#2  
DarkThanos's Avatar
Senior Member - OP
Thanks Meter 41
Posts: 210
Join Date: Mar 2011
Location: Chania

 
DONATE TO ME

All Scripts
Links to all scripts posts
SOME PEOPLE ONLY SEE WORDS (⊙.⊙(☉_☉)⊙.⊙) BUT OTHER PEOPLE DO NOT ONLY SEE, THEY READ TOO
READING IS A GIFT NOWADAYS... ARE YOU GIFTED ?
The Following User Says Thank You to DarkThanos For This Useful Post: [ Click to Expand ]
 
DarkThanos
Old
(Last edited by DarkThanos; 25th August 2014 at 05:54 PM.)
#3  
DarkThanos's Avatar
Senior Member - OP
Thanks Meter 41
Posts: 210
Join Date: Mar 2011
Location: Chania

 
DONATE TO ME

[Script][Swap External SD with Internal SD][v1.0]
Quote:
  • Name : Swap External SD with Internal SD
  • Version : v1.0
  • Author : DarkThanos
  • Thanks to: codlab
This script wait for your external sd card to be mounted and swap it with internal if it is bigger.
One minute after boot it will stop waiting.
(Fastest swap method I found)

I am not responsible for whatever happens to your phone.
Tested on my Samsung Galaxy Core (I8260)

[Change log]
 

Quote:
  • 1.0 Version
    • Script Public for first time.
    • Swap internal sd with external (if external>internal)

[Dev Doc]
 

Quote:
The script first creates 2 directories to mount external sd and internal sd,
so that it can distinguish them from each other ("/data/sd_cards/internal/", "/data/sd_cards/external/").
Code:
# Mount root rw
mount -o remount,rw /

# Create directory to mount sd cards
	# Make directory to save cards
	mkdir -p /data/sd_cards

	# Make directory to save internal
	mkdir -p /data/sd_cards/internal

	# Make directory to save external
	mkdir -p /data/sd_cards/external
In the next step, the script waits for an external sd to be mounted.
This wait time is the reason why the script is better to be asynchronous.
The script checks every 5 seconds for an external sd, but after 1 minute (12 tries) it stops.
Code:
# Wait for external sd card to be mounted
	# Max wait time is 1 minute
	maxWaitTime=60
	# While external Sd Card has 0 Capacity
	while [[ $(du -s /storage/extSdCard | awk '{print $1}') == "0" && $maxWaitTime>0 ]]
	do
		# Check again in 5 seconds
		sleep 5
		# Count down max wait time
		maxWaitTime=maxWaitTime-5
	done
As soon as an external sd is mounted, the script mounts the external sd on "/data/sd_cards/external/"
and the internal on "/data/sd_cards/internal/".
Code:
# Save mount points on folders
	# Mount bind internal SD in folder /data/sd_cards/internal
	mount -o bind /storage/sdcard0 /data/sd_cards/internal

	# Mount bind external SD in folder /data/sd_cards/external
	mount -t vfat -o rw,nosuid,nodev,noexec,relatime,user_id=1023,group_id=1023,default_permissions,allow_other /dev/block/vold/179:33 /data/sd_cards/external
So now the real internal sd can be found in the directory "/data/sd_cards/internal/"
and the real external sd can be found in the directory "/data/sd_cards/external/".
The reason we do this is to be able to make a script swap them again.

Then it checks the size of each, so that it knows if it ia worthing of swapping them.
Code:
# Get sd cards sizes
	internalSdSize=$(du -s /storage/sdcard0 | awk '{print $1}')
	externalSdSize=$(du -s /storage/extSdCard | awk '{print $1}')
If external sd is bigger, then on "/storage/sdcard0/" we mount "/data/sd_cards/external/"
and on "/storage/extSdCard/" we mount "/data/sd_cards/internal/".
Code:
# Swap sd cards
	# If external sd card is bigger than internal swap 
	if [ $externalSdSize -gt $internalSdSize ]
	then
		# Swap
		mount -o bind /data/sd_cards/internal /storage/extSdCard
		mount -o bind /data/sd_cards/external /storage/sdcard0
	fi


Attached Files
File Type: zip v1.0.darkthanos.primary.sd.async.zip - [Click for QR Code] (988 Bytes, 4 views)
SOME PEOPLE ONLY SEE WORDS (⊙.⊙(☉_☉)⊙.⊙) BUT OTHER PEOPLE DO NOT ONLY SEE, THEY READ TOO
READING IS A GIFT NOWADAYS... ARE YOU GIFTED ?
The Following User Says Thank You to DarkThanos For This Useful Post: [ Click to Expand ]
 
DarkThanos
Old
(Last edited by DarkThanos; 25th August 2014 at 06:42 PM.)
#4  
DarkThanos's Avatar
Senior Member - OP
Thanks Meter 41
Posts: 210
Join Date: Mar 2011
Location: Chania

 
DONATE TO ME

[Script][Optimize SQlite databases of apps][v1.0]
Quote:
  • Name : Optimize SQlite databases of apps
  • Version : v1.0
  • Rewritten by : DarkThanos
  • Based on work of: pikachu01
This script optimize applications' database using sqlite3 (Vacuum/Reindex) every 5 boots or 5 days.
On any other boot it optimize only databases of phone, contacts, mms and settings .

Before use make a folder "libs" inside "/system/etc/init.d/",
insert the "sqlite3" file inside and give it execute permissions (the file is given with the script)

Bugs: (Its safe for use, its a warning not an error)
Contacts database is always locked, so can't be optimized.

I am not responsible for whatever happens to your phone.
Tested on my Samsung Galaxy Core (I8260)

[Change log]
 

Quote:
  • 1.0 Version
    • Script Public for first time.
    • SQlite databases on "/data/" optimize every 5 boots or 5 days.
    • SQlite databases of phone, contacts, mms and settings optimize on every boot.

[Dev Doc]
 

Quote:
Script first initiate some variables
(path to Sqlite lib, path to a folder to save scripts data, minimum time between optimizes)
Code:
# Some variables
	# SQLite path
	sql_lib="/system/etc/init.d/libs/sqlite3";
	# Data folder path
	data_folder="/data/boot_scripts_data/";
	# Data counter file name
	data_file_counter="sql_optimize_counter";
	# Data last run file name
	data_file_lastrun="sql_optimize_lastrun";
	# Max time between optimizes (in sec)
	# 5 days = $(expr 5 \* 24 \* 60 \* 60 )
	max_time_dif=432000
Check how many boots passed since last optimize
and when the last optimize was by reading scripts data.
Code:
# Create data folder if not exist
	mkdir -p "$data_folder"
# Get data from files
	# Run Counter
	if [[ ! -f "$data_folder$data_file_counter" ]]
	then
		# File do not exist
		touch "$data_folder$data_file_counter"
		echo "0" > "$data_folder$data_file_counter"
		runCount="0"
	else
		runCount=$(cat $data_folder$data_file_counter)
	fi
	# Last run date
	if [[ ! -f "$data_folder$data_file_lastrun" ]]
	then
		# File do not exist
		touch "$data_folder$data_file_lastrun"
		echo "0" > "$data_folder$data_file_lastrun"
		lastRun=0
	else
		lastRun=$(cat $data_folder$data_file_lastrun)
	fi
If boots passed since last optimize equals with 5 or last optimized more than 5 days before,
then make a full database optimization.
Code:
# Check if its time for optimize
	if [ $(( $(date +%s) - $lastRun )) -ge $max_time_dif -o $runCount -ge 5 ]; then
		echo "Full mode optimize"
		
		# Save data
		echo "0" > "$data_folder$data_file_counter"
		echo $(date +%s) > "$data_folder$data_file_lastrun"
		
		# Optimize /data/ 
		optimize_folder "/data/"
		
		echo "All applications' databases were optimized"
Else make a light optimize.
(only phone, contacts, mms and settings)
Code:
	else
		echo "Light mode optimize"
		
		# Save data
		echo $((runCount+1)) > "$data_folder$data_file_counter"
		
		# Only for Galaxy Core
			# Count databases optimized
			count=0
			
			# phone optimize
			optimize_folder "/data/data/com.android.phone/databases/"
			# contacts optimize
			optimize_folder "/data/data/com.android.providers.contacts/databases/"
			# mms optimize
			optimize_folder "/data/data/com.android.mms/databases/"
			# email optimize
			optimize_folder "/data/data/com.android.mms/databases/"
			# settings optimize
			optimize_folder "/data/data/com.android.settings/databases/"
		
			echo "Important databases were optimized"
	fi;
Optimize functions
Code:
# Some functions
	# Optimize db
	optimize_db(){
		# Vacuum and then reindex
		$sql_lib $1 'VACUUM;';
		$sql_lib $1 'REINDEX;';
		#echo $(basename $1)" was optimized."
	}
	# Optimize every db on folder
	optimize_folder(){
		local path=$1
		count_result=0;
		find $path -type f -iname "*.db" -print0 | while IFS= read -r -d $'\0' database; do
			optimize_db $database
		done
		#echo "Databases inside $path were optimized."
	}
Use the sqlite binary to VACUUM and REINDEX the given database.

Attached Files
File Type: zip v1.0.darkthanos.sqlite.optimize.zip - [Click for QR Code] (19.5 KB, 1 views)
SOME PEOPLE ONLY SEE WORDS (⊙.⊙(☉_☉)⊙.⊙) BUT OTHER PEOPLE DO NOT ONLY SEE, THEY READ TOO
READING IS A GIFT NOWADAYS... ARE YOU GIFTED ?
The Following User Says Thank You to DarkThanos For This Useful Post: [ Click to Expand ]
 
DarkThanos
Old
(Last edited by DarkThanos; 25th August 2014 at 06:57 PM.)
#5  
DarkThanos's Avatar
Senior Member - OP
Thanks Meter 41
Posts: 210
Join Date: Mar 2011
Location: Chania

 
DONATE TO ME

[Script][Optimize apks using ZipAlign][v1.0]
Quote:
  • Name : Optimize apks using ZipAlign
  • Version : v1.0
  • Author : Wes Garner
  • Suggestions : oknowton
  • Edited by : DarkThanos
This script zipalign applications' apk (only the apks that are not already zipaligned) on every boot.
First boot may take a while.

Before use make a folder "libs" inside "/system/etc/init.d/",
insert the "zipalign" file inside and give it execute permissions (the file is given with the script)

Bugs:(Not for use, more testing needed)
Some apps return an error after zipaligned, and need to be reinstalled.
(Found fix. Zipaligned apk do not holds read rights after copy. Command "cp -p" do not hold permissions. Temp fix "chmod 644". [I need help with that])

A download will be posted when it is stable.

I am not responsible for whatever happens to your phone.
Tested on my Samsung Galaxy Core (I8260)
Need to be tested for stable use.

[Change log]
 

Quote:
  • 1.0 Version
    • Script Public for first time.
    • Optimize apks using ZipAlign (on every boot check apps).

[Dev Doc]
 

Quote:
Script code
Code:
#!/system/bin/sh
# Optimize apks using ZipAlign
#
# Work of:
# 	Wes Garner	(Script code)
#	oknowton	(Suggestions)
#
# Modifications by:
#	DarkThanos
#
# For the xda Galaxy Core users
#

# Some variables
	# SQLite path
	zipalign_lib="/system/etc/init.d/libs/zipalign";
	# Data folder path
	data_folder="/data/boot_scripts_data/";
	# Data database file name
	apk_cache="zipalign_apk_cache";
	# Counters
	count_apps=0
	count_zipaligned_apps=0
	count_zipalign_failed=0
	
	# Report ZipAlign start
	echo "ZipAlign Optimization Start";
	
	# For every apk in /data/app/
    for apk in /data/app/*.apk ; do
	
		# Check if this apk is zipaligned
		$zipalign_lib -c 4 $apk;
		# Get results
		not_zipaligned=$?;
		# Count app
		count_apps=$((count_apps+1))
		
		# If not, align it
		if [ $not_zipaligned -eq 1 ]; then
			# ZipAlign app
			result=`$zipalign_lib -f 4 $apk $data_folder$apk_cache`
			if [ -e $data_folder$apk_cache -a ${#result} -eq 0 ]; then
				# Successful zipalign
				/system/xbin/busybox cp -p $data_folder$apk_cache $apk;
				chmod 644 $apk;
				rm $data_folder$apk_cache;
				count_zipaligned_apps=$((count_zipaligned_apps+1))
			else
				echo "[ERROR] $result"
				# Failed zipalign
				count_zipalign_failed=$((count_zipalign_failed+1))
				#echo ZipAligning $(basename $apk) Failed  | tee -a $LOG_FILE;
			fi;
		fi;
    done;
	
	# Report results
	echo "Apks ZipAligned $count_zipaligned_apps/$count_apps ($count_zipalign_failed failed)";

	# Exit
	exit 0
I need help with the code:
Code:
/system/xbin/busybox cp -p $data_folder$apk_cache $apk;
chmod 644 $apk;
rm $data_folder$apk_cache;
I try to make all my script work with no busybox. "cp" command do not exist in android (without busybox).
Is "cat" a good alternative?

Also, should I use "chmod 644 $apk;"? "cp -p" do not work and I am pretty sure the script has root rights.

Thanks for your time.

SOME PEOPLE ONLY SEE WORDS (⊙.⊙(☉_☉)⊙.⊙) BUT OTHER PEOPLE DO NOT ONLY SEE, THEY READ TOO
READING IS A GIFT NOWADAYS... ARE YOU GIFTED ?
The Following User Says Thank You to DarkThanos For This Useful Post: [ Click to Expand ]
 
NieeLz
Old
#6  
NieeLz's Avatar
Member
Thanks Meter 4
Posts: 84
Join Date: Sep 2013
Location: Jakarta
I need betatester

Sent from my GT-I8262 using XDA Premium 4 mobile app
 
DarkThanos
Old
#7  
DarkThanos's Avatar
Senior Member - OP
Thanks Meter 41
Posts: 210
Join Date: Mar 2011
Location: Chania

 
DONATE TO ME
Quote:
Originally Posted by NieeLz View Post
I need betatester
Download the "init.bsu.boot" script (from first post). Create the "/system/etc/init.d/" folder. And run the script (from your download folder) using Script Manager (do not forget to enable "su"). Then check "/data/tmp/init.bsu.log" (open as text) and see the log.

This is an easy safe test.
You can also make a test script, like:
Code:
#!/system/bin/sh
# Test Script
echo "This will be on log."
(You can read the script codes too, I have many comments.)
Attached Files
File Type: zip test.script.zip - [Click for QR Code] (223 Bytes, 2 views)
SOME PEOPLE ONLY SEE WORDS (⊙.⊙(☉_☉)⊙.⊙) BUT OTHER PEOPLE DO NOT ONLY SEE, THEY READ TOO
READING IS A GIFT NOWADAYS... ARE YOU GIFTED ?

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes