Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,806,858 Members 36,564 Now Online
XDA Developers Android and Mobile Development Forum

[Guide] [Dec.15] How to Tweak and Mod Android Using Init.d Scripts

Tip us?
 
lucius.zen
Old
(Last edited by lucius.zen; 22nd May 2014 at 06:05 AM.) Reason: cleaned intro and basics section
#1  
lucius.zen's Avatar
Senior Member - OP
Thanks Meter 415
Posts: 453
Join Date: Sep 2012
Location: Halifax
Tutorial [Guide] [Dec.15] How to Tweak and Mod Android Using Init.d Scripts

Hello my fellow Android enthusiasts,

There are many apps that can be used to tweak kernel settings. However, I am a reductionist at heart and prefer to break things down to their essential parts. This guide was written for those who want to be rid of apps that dig into their kernel at every boot. It is a simple guide to writing init.d scripts that the average user should be able to implement.

Happy Tweaking,

Lucius

PS......ITS A CELEBRATION, BITCHES!!!!!!!


Basics:
The android boot sequence is divided into 3 parts.

1) First stage bootloader
2) Kernel loads and prepares hardware
3) User space programs load - this is where init.d scripts are invoked

Init.d scripts execute commands as if they were entered into a terminal. These commands modify the value of parameters that influence our devices behaviour. They are much more flexible than tweaking apps and give end users the ability to tweak and optimize their device as they see fit. They are small, fast, clean... and awesome.


Requirements:
See my guide to modding and optimization for instructions on how to unlock, install TWRP, root, and install custom ROMs/kernels - http://forum.xda-developers.com/show....php?t=2232715

Unlocked: Needed to install custom recovery.

Custom Recovery: Needed to install custom ROMs and custom kernels.

Root Permissions: Needed to access /system partition - enabled when most custom ROMs are installed

Init.d Support: Needed to run init.d scripts at boot - enabled in every custom ROM/kernel I have come across.

Scripting Tool: Text editor or script manager - I use Android Tuner to create/test a script and AntTek File Explorer Ex's built in text editor to edit a script.

Root File Manager: I use AntTek Explorer Ex, it reminds me of my old linux setup, its very functional but clean as hell. Also has a built-in text editor.


Step #1: Acquire a Scripting Tool
I use Android Tuner for script creating/testing. You could also use a text editor. AntTek Explorer Ex has a built in text editor that works well. You could also use Notepad++, its a free linux based text editor. Just avoid windows-based editors, they leave extra spaces and invisible characters when enter is pressed.


Step #2: Begin Your Script
To create a script open your text editor or select "create new script" within the "script manager" in Android Tuner. Put your cursor at the very top left corner to begin writing. The first line of a script must invoke a compatible shell that will execute the script, which in our case is the following:

#!/system/bin/sh

Once this line is typed simply hit enter to start a new line. I usually put an empty line after the first line as it makes things easier to see. Extra lines for spacing purposes are fine so long as they are completely empty.

NOTE - Android tuner automatically appends the above line to the beginning of all scripts. It will not be displayed in the script creator but can be found in the final script you save.

TIP - Do not add spaces at the end of a line. Syntax must be exact.

TIP - Adding a "#" to the front of the line instructs the shell to avoid this line, which is essential for writing notes and keeping scripts organized.


Step #3: Write Your Tweaks
Now we can start writing tweaks. If written correctly each line that you enter will execute a command that changes a kernel or system level parameter. This seems daunting at first but it is really quite easy. Once you are finished typing a line simply hit enter to begin the next line. Here is an example of an executable command so you know what we are talking about going forward.

echo "SmartAssv2" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

NOTE - For some examples of useful tweaks see my guide to tweaking and optimization - http://forum.xda-developers.com/show....php?t=2232715

GENERAL FORMAT:
The tweaks that we are interested in typically begin by identifying the value to be written to the desired parameter:

echo "parameter value" >

The new parameter value is always followed by the directory tree locating the parameter of interest:

/sys/devices/system/cpu/cpu0/cpufreq/parameter file

In order to create:

echo "700" > /sys/devices/system/cpu/cpu0/cpufreq/gpu_oc

CPU/GPU TWEAKS:
To familiarize yourself with how tweaking is done I would start by opening up the following directory in your file manager:

/sys/devices/system/cpu/cpu0/cpufreq/

This is where cpu parameters, and often gpu parameters, are located. These files/parameters determine how our kernel controls our CPU. To modify our CPU's behaviour these parameters are modified at boot utilizing init.d scripts. Familiarize yourself with these files, open them up with a text editor - it's entirely safe so long as you don't start changing values. If the name of a parameter isn't self-explanatory, opening it up and examining the value it holds will typically explain its purpose. Here are some common parameters that are useful to know.

scaling_available_freq - lists the frequencies available to the cpu
scaling_available_governors - lists the cpu governors available for use
scaling_governor - sets the cpu governor
scaling_max_freq - sets the max freq
scaling_min_freq - sets the min freq
UV_mV_table - sets the voltage table for the cpu

GOVERNOR TWEAKS:
The CPU governor determines how our CPU behaves in response to changes in workload. Changing the governor will impact how our CPU scales through the freq steps available to our CPU. This has a significant impact on the time our CPU spends at different frequency steps, which influences efficiency, performance, battery life, and stability. For more info about the various governors available in android see post #1 at the following link - http://forum.xda-developers.com/show....php?t=1369817

Tweakable governor parameters are located in:

sys/devices/system/cpu/cpufreq/

Various interactive governor parameters can be accessed, such as:

go_hispeed_load - Go to hispeed_freq when cpu load at or above this value
hispeed_freq - freq to bump to once go_hispeed_load is passed
min_sample_time - minimum amount of time that can be spent at a frequency before scaling down
timer_rate - the interactive governor checks cpu load at regular intervals to determine whether frequencies should be increased when it comes out of idle. If the cpu is busy between exiting idle and the timer firing we assume the cpu is underpowered and ramp frequency up

HOTPLUG CONFIG:
The hotplug config in Hundsbuah's Kernel allows you to control how CPU cores are brought online as threads are processed. A thread is the smallest sequence of instructions that can be managed independently by a process scheduler. Threads are contained within a process. A process is the execution of instructions contained within a program. On a single processor system multi-threading (multitasking) is generally implemented by transmitting/sending signals over a common signal path; the processor switches between different threads. This switching generally happens frequently enough that the user perceives the threads or tasks as running at the same time. If the CPU is overloaded and a thread is queued up by the process scheduler then lag/stuttering is likely to occur because thread switching does not occur quickly enough to be hidden from the user. On a multi-core system threads can be truly concurrent, with every processor or core executing a separate thread simultaneously, which decreases the potential for lag/stuttering. If core 1 is busy processing a thread and another thread is queued up by the process scheduler we want an additional core to become active so that core 1 does not have to switch between threads. However, we also do not want to bring cores online needlessly. If a core is able to process multiple threads fast enough such that switching is unnoticeable then it would be inefficient to bring another core online. The hotplug config can be found in the following location:

/sys/kernel/rt_config/rt_config

I/O TWEAKS:
The I/O scheduler handles read/write requests from memory (RAM and storage). The I/O scheduler we use can significantly impact I/O (read/write) performance. For more info about the various I/O schedulers available in android see post #4 in the following thread - http://forum.xda-developers.com/show....php?t=1369817

The location of the I/O scheduler is:

/sys/block/mmcblk0/queue/scheduler
/sys/block/mmcblk1/queue/scheduler

Use the follIng lines to change the I/O scheduler:

echo "sio" > /sys/block/mmcblk0/queue/scheduler
echo "sio" > /sys/block/mmcblk1/queue/scheduler

KERNEL MODULES:
A kernel module is a file containing code that extends the functionality of our kernel. Modules allow our kernel to communicate with different types of hardware (ex - bluetooth), utilize various file systems (ex - NTFS), or system calls (ex - frandom.ko). Kernels do not automatically come with all available modules, loading only the most essential modules can reduce a kernels footprint and potentially improve performance. Simultaneously, loading improved modules can improve performance in some cases (ex - frandom.ko). For more info about various kernel modules see post #3 in the following thread - http://forum.xda-developers.com/show....php?t=1369817

NOTE - You cannot download and load kernel modules from the internet. They need to be compiled for kernel you are using.

To make a kernel module available make sure /system is mounted as rewritable, rather than read-only (see step #5 for further instructions). Copy the module .ko file (ex - tntfs.ko) to the following location:

/system/lib/modules

To check what modules are loaded at boot enter the following command in a terminal:

lsmod

To load a kernel module at boot add the following line to your init.d script:

insmod /system/lib/modules/module name

To unload a kernel module at boot add the following line to your script

rmmod module name

SYSCTL TWEAKS:
This section is currently under construction, more tweaks will be posted once I find them. Sysctl is an interface that is used to examine and dynamically change parameters within a linux based OS.

To change the TCP Congestion Algorithm add the following line to your script:

/system/xbin/sysctl -w net.ipv4.tcp_congestion_control=westwood

NOTE - For a brief explanation of TCP/IP protocols and network congestion algorithms see the following post - http://forum.xda-developers.com/show...postcount=1884

FINISHING YOUR SCRIPT:
Once all your tweaks are written go through the entire script and make sure syntax is exact - no extra spaces or characters. Save your first script as w/e you want (ill explain naming later) just make sure it doesn't have a file extension. I'm talking total absence of file extensions. WHOA! WHAT! WTF YO!

NOTE - Android Tuner saves scripts by default in the "at" folder in "sdcard0"

Example Script:
Code:
#!/system/bin/sh

#CPU Tweaks
echo "1350 1320 1280 1240 1200 1150 1100 1050 1000 950 900 860 820 780 750 720 700" > /sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table

#Governor Tweaks
echo "40000" > /sys/devices/system/cpu/cpufreq/interactive/min_sample_time 

#SD Tweaks
echo "vr" > /sys/block/mmcblk0/queue/scheduler
echo "vr" > /sys/block/mmcblk1/queue/scheduler

#Module Tweaks
insmod /system/lib/modules/ frandom.ko
rmmod tntfs.ko
rmmod texfat.ko

Step #4: Test Your Script
You can test your script using a root terminal shell (Android Tuner has a terminal). Open a terminal, type "su", and hit enter (or select the box beside "su" in AT). This will give you the ability to run commands as root. To test your script enter the following command:

sh directory of your script

EXAMPLE - sh /storage/sdcard0/at/scripts/01ZenKernelTweaks

Check the files that correspond to the tweaked parameters to see if the script worked. If the values were correctly modified then you have officially finished writing your first working init.d script.


Step #5: Enable Your Script
First, open your file manager, go to your root directory, and remount your /system partition as RW instead of RO. This is typically done by long-pressing the /system folder (ex - AntTek Explorer), file manager settings (ex - esFile Explorer), or through the overflow menu. Once the /system is mounted as RW we can move our script to the init.d folder and change our scripts permissions. The init.d folder is located here:

/system/etc/init.d

The name of your script determines the order in which it is executed. The first two characters determine the order of execution. The ordering is as follows - numbers from 0-99, then letters from A-Z. The first two letters of a script's name are typically numbers between 0-99, this is the standard method of script naming that most developers/tweakers use. Set the name of your script so that it is executed in the order that you wish. Mine is set to execute first because I want to get kernel tweaks done as early as possible.

Once you have named your script long-press it and select "Change Permissions" (or however this is done in your file manager). Give User, Group, and Other read, write, and execute permissions.

Your init.d script will now optimize your tablet quickly and efficiently at every boot. Don't get out of your chair too fast....Before doing anything else, I suggest revelling in your awesome new-found abilities for a significant period of time. Pat yourself on the back. Start referring to yourself as "a boss" if you like. You deserve it.


CAUTION:
Although init.d scripts are easy to make, a bad script may cause boot looping. However, a bad script does not necessarily = boot looping. I have yet to cause a boot loop and I have written many scripts that did not work. Everything typically boots normally, the bad lines in the script are simply not applied. Before enabling your script I strongly suggest taking a Nandroid backup in TWRP. To do so boot into TWRP and select "Backup" from the home screen. This will allow you to restore your entire system, which is done via the "Restore" tool in TWRP.

NOTE - Do not enable scripts to run at boot in a program like Android Tuner or others similar to it. If the script is bad it will cause boot looping because the app will keep trying to run it. If you put the script in the init.d folder and enable it yourself you will avoid this issue.


SUGGESTION:
If you want more examples of tweaks that can help you optimize your tablet see my guide to tweaking and optimization. It fills any gaps in this guide and contains many useful resources. You can find it here - http://forum.xda-developers.com/show....php?t=2232715
Devices:
Asus Transformer tf701t
- ROM - Cromi-X
- Kernel - Cromi-X Kernel
- Mods - CrossBreeder, Bravia Engine 2
- Xposed - GravityBox, LWInRecents, SwipeBack, XHaloFloatingWindow
Asus tf700t
- Retired
HTC One M7
- Stock
Rikomagic MK802 IV

Guides:
How to Tweak and Mod Android Using init.d Scripts - http://forum.xda-developers.com/show....php?t=2198510
How to Tweak and Optimize Android Performance - http://forum.xda-developers.com/show....php?t=2232715
The Following 53 Users Say Thank You to lucius.zen For This Useful Post: [ Click to Expand ]
 
snypa
Old
#2  
snypa's Avatar
Senior Member
Thanks Meter 59
Posts: 217
Join Date: Nov 2012
Angry tweakalicous...

Quote:
Originally Posted by lucius.zen View Post
HAPPY TWEAKING,

Lucius


PS......ITS A CELEBRATION, BITCHES!!!!!
LOL ... great tut Zen ...
The Following User Says Thank You to snypa For This Useful Post: [ Click to Expand ]
 
iamborba
Old
#3  
Member
Thanks Meter 15
Posts: 77
Join Date: Feb 2013
Very nice, thanks Zen.
 
lucius.zen
Old
#4  
lucius.zen's Avatar
Senior Member - OP
Thanks Meter 415
Posts: 453
Join Date: Sep 2012
Location: Halifax
I accidentally stated in Step #3 that "Android saves scripts by default in the 'at' folder in your internal storage"....However, I meant to write "Android Tuner saves scripts...". Changed the OP, just wanted to make sure anyone who has read it already isnt like "WTF ZEN! Where is my damn script! Derpa a derrrr".
The Following User Says Thank You to lucius.zen For This Useful Post: [ Click to Expand ]
 
lucius.zen
Old
(Last edited by lucius.zen; 20th March 2013 at 05:03 AM.)
#5  
lucius.zen's Avatar
Senior Member - OP
Thanks Meter 415
Posts: 453
Join Date: Sep 2012
Location: Halifax
Default Breaking News.....

***************IMPORTANT UPDATE - MUST READ BREAKING NEWS*****************


DEY TUK ARRRRR JEEEERRRRRRRBSSSSS!!!!!!!





.....watching too many episodes of SP lately
The Following User Says Thank You to lucius.zen For This Useful Post: [ Click to Expand ]
 
_that
Old
#6  
Recognized Contributor
Thanks Meter 2,415
Posts: 3,427
Join Date: Oct 2012
Quote:
Originally Posted by lucius.zen View Post
How-To Write Init.d Scripts for the tf700
Great tutorial!

Quote:
Originally Posted by lucius.zen View Post
The number, or letter, at the beggining of the init.d script determines its order of sexecution, beginning at 1 or A, and going up to 99 or Z.
The universal convention is to use a numeric prefix from 00 to 99, but in reality the name can be anything and the scripts are simply executed in alphanumeric order - digits come before "A". I recommend using two-digit prefixes as intended.

Quote:
Originally Posted by lucius.zen View Post
Code:
#!/system/bin/sh

#CPU Tweaks
echo "1300 1200 1150 1125 1112 1100 1087 1075 1050 1037 1025 1012 987 975 962 950 925 912 900 887 875 862 850 837 825 812 800 775 762 750 737 725 712 700 687 675 650 637 625 600" > /sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table
Note that the UV_mV_table has a completely different format when reading or writing the file - one reason why I hate this interface. But it's the "standard" that was established by whoever hacked the first implementation, and user mode apps expect it like this.

Quote:
Originally Posted by lucius.zen View Post
Although init.d scripts are easy to make, you can potentially cause boot looping, please proceed with caution.
To repair this, boot into recovery, connect via adb shell, and do the following:

(if needed
mount -o remount,rw /system

chmod 0666 /system/etc/init.d/50myevilscript

This will remove "execute" permissions for the script, which will effectively disable it.
The Following 4 Users Say Thank You to _that For This Useful Post: [ Click to Expand ]
 
TunedFish
Old
#7  
TunedFish's Avatar
Senior Member
Thanks Meter 33
Posts: 167
Join Date: Aug 2010
Say I tested and am currently using 10 scripts, how do I create a .zip recovery file so I can flash/install the scripts at once, every time I reflash or factory reset my rom?
 
SynnyG
Old
#8  
SynnyG's Avatar
Senior Member
Thanks Meter 177
Posts: 647
Join Date: Jan 2013
Location: Strasbourg

 
DONATE TO ME
Thanks for your awesome tuto ! But I've a question concerning the gpu governor: it's possible to set this governor with command line too ?
 
LetMeKnow
Old
#9  
Senior Member
Thanks Meter 593
Posts: 1,466
Join Date: Jun 2013
@lucius.zen

It is a nice write up and great tutorial. I am enjoy reading it and keep up the good work...
The Following User Says Thank You to LetMeKnow For This Useful Post: [ Click to Expand ]
 
lucius.zen
Old
#10  
lucius.zen's Avatar
Senior Member - OP
Thanks Meter 415
Posts: 453
Join Date: Sep 2012
Location: Halifax
Quote:
Originally Posted by LetMeKnow View Post
@lucius.zen

It is a nice write up and great tutorial. I am enjoy reading it and keep up the good work...
@LetMeKnow

Thanks a lot dude very much appreciated, makes me miss contributing to xda. I made a lot of promises and left too much work unfinished. I haven't been on for 6 months because I have been dealing with some pretty serious mental health issues. Apparently I am mildly schizophrenic with comorbid anxiety and attention issues. Been super fun times lol. Hoping to get back in the game this holiday season, would be sweet to share some tweaking/optimization ideas, I havent had much of a chance to check out your tweaks but I have heard great things.

All the best,

Lucius

The Following User Says Thank You to lucius.zen For This Useful Post: [ Click to Expand ]
Tags
android, init.d, script, tf700, tweak
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes