[HOWTO] Add init.d to stock ROM + a few other goodies

Search This thread

wd5gnr

Senior Member
Dec 20, 2010
460
130
Houston
I have been resisting the urge to flash a custom ROM for a bit, but I really miss having init.d support. So I read a few threads for other phones and rolled my own.

Warnings
I borrowed bits and pieces from various places. If you don't know what init.d is, you probably don't want to do this. If you aren't willing to take responsibility for bricking your tablet, don't do this. Seriously, the risk of bricking is very low, but if you aren't comfortable booting into an adb shell from recovery, maybe this is not for you. Strongly suggest a nandroid backup before you get started so if you totally bork things you can just hit rewind.

Note: The latest CWM may prompt you on a reboot that the ROM may overwrite the bootloader and offer to fix it for you. Don't do that. The init.d hack takes over the bootloader install script, but does not change your bootloader! If you accidentally do let it fix things for you, just rebuild the install-bootloader.sh file. The other steps should be fine.

Prerequisites
First, you need root, busybox, and some sort of terminal (either adb, or some terminal you like using on the tablet).
I have found that I like Busybox Installer (from the market; https://play.google.com/store/apps/details?id=com.jrummy.busybox.installer) but for some reason it doesn't create new symlinks unless you click advanced install.

Let's get to it!
In the shell (don't type # or anything after #):

Code:
su       # get root
mount -o remount,rw /system     # get access to /system (4.04 seems to mount ro as is usual; seems like the original mounted rw)
which run-parts     # if you don't see /system/xbin/run-parts you need to install/reinstall busybox; if it is somewhere else, note it
mkdir /system/etc/init.d

Create a file called sysinit -- we are going to put it in /system/bin. You can edit it in place with vi, mount your tablet and edit it on your computer, or create it on the computer and push it via adb. Whatever.

Here's the file (you do need the # and the things after it in the file!):
Code:
#!/system/bin/sh
export PATH=/sbin:/system/sbin:/system/bin:/system/xbin
/system/bin/logwrapper /system/xbin/run-parts /system/etc/init.d

Note that if your run-parts is not in /system/xbin (from the which command) then fix the above to reflect your reality.

In the shell, make it executable
Code:
chmod 755 /system/bin/sysinit

Now go in the init.d directory and create some things you want to run at start up. For example:
Code:
cd /system/etc/init.d
echo '#!/system/bin/sh' >99test    # note: you do need the first # in this line but not the 2nd!
echo 'date >>/data/tmp/init.d-log.txt' >>99test
chmod 755 99test

Here's a more practical one (yes, you need the # signs). Name it something like 10diskperf -- don't forget to chmod it.
Code:
#!/system/bin/sh
# Set disk read aheads to 1024
chmod 777 /sys/block/mmcblk0/queue/read_ahead_kb
echo "1024" > /sys/block/mmcblk0/queue/read_ahead_kb
chmod 777 /sys/block/mmcblk1/queue/read_ahead_kb
echo "1024" > /sys/block/mmcblk1/queue/read_ahead_kb
chmod 777 /sys/devices/virtual/bdi/179:0/read_ahead_kb
echo "1024" > /sys/devices/virtual/bdi/179:0/read_ahead_kb

Or here is one to tweak some TCP parameters (25sysctl):
Code:
#!/system/bin/sh
sysctl -w net.core.rmem_max=524288
sysctl -w net.core.wmem_max=524288
sysctl -w net.ipv4.tcp_rmem=6144 87380 524288
sysctl -w net.ipv4.tcp_wmem=6144 87380 524288

Whatever files you put in, you need to remember to make them executable (chmod 755).

Finally, you need to kick it all off at start up. The hack for that is we are going to create /system/etc/install-recovery.sh which apparently runs on each boot.


Code:
cd /system/etc
echo '#!/system/bin/sh' >install-recovery.sh
echo '/system/bin/sysinit' >>install-recovery.sh
chmod 755 install-recovery.sh

Tips and troubleshooting

If you are too lazy to cut and paste I have the files here (View attachment init.d-support.zip) that you can just move to the right places and change permission. If you are really lazy there is lightly tested install script below.

I like to try running the whole thing before a reboot to see if I get any errors:

Code:
/system/etc/install-recovery.sh

I'd suggest putting the 99test file in first. Verify that you get the expected file in /data/tmp and then reboot and check again. Then you can remove 99test.

Same goes for adding new scripts. Try running them from the shell to see if they throw errors before you reboot!

If you have trouble, see if this looks right:
Code:
ls -ld /system/etc/install-recovery.sh /system/bin/sysinit /system/etc/init.d /system/xbin/run-parts
-rwxr-xr-x root     root           39 2012-07-14 10:00 install-recovery.sh
-rwxr-xr-x root     root          140 2012-07-14 10:01 sysinit
drwxrwxrwx root     root              2012-07-14 10:10 init.d
lrwxrwxrwx root     root              2012-07-14 09:55 run-parts -> /system/xbin/busybox

For the brave
The install-init.d zip file (View attachment install-init.d.zip) contains a lightly tested script that SHOULD do the install steps for you.
Send the file to your android to someplace that can execute code (e.g., /system/xbin; I had to use adb to put it on the sdcard and then move it to /systemxbin in the shell since I don't have the adb root kernel installed).
Code:
cd /system/xbin     # or wherever you have it
chmod 755 install-init.d
./install-init.d

It performs rude checks to see if init.d exists, and tries to handle moving or missing busybox. It only installs 99test as a script.

Let me know if this works or doesn't work for you.

For the extra brave: There is no reason this should only work on the Samsung. This ought to work on pretty much most stock ROMs as long as they execute install-recovery.sh on start up.


Scripts

What do you put in your init.d? If you post anything cool I'll put it up here in the op.

  • One that gave me some real gains in I/O performance required a new version of the tune2fs executable. By default, it is part of busybox but the busybox one only has a few simple options. I've included a stand alone version and the script 10disktune here View attachment disktune.zip. Unpack the zip and put the 10disktune in /system/etc/init.d (don't forget to chmod) and put tune2fs in /system/bin (chmod that too). Note that busybox has one in /system/xbin but the script specifically calls out the one in /system/bin.
  • Here's one that will zipalign your apks on each boot
    Code:
    #!/system/bin/sh
    for apk in /data/app/*.apk ; do
      zipalign -c 4 $apk
      ZCHECK=$?
      if [ $ZCHECK -eq 1 ]; then
        zipalign -f 4 $apk /cache/$(basename $apk)
        if [ -e /cache/$(basename $apk) ]; then
          cp -p -f /cache/$(basename $apk) $apk
          rm /cache/$(basename $apk)
        fi;
      fi;
    done;



Fin

Corrections welcome. I considered using exec or . to load some of this into one shell but given that it runs once at startup, I figured it is fine as is.

All files for reference

 
Last edited:

wd5gnr

Senior Member
Dec 20, 2010
460
130
Houston
I did it on my Galaxy Nexus.
It works great, I had a bit of problem with the sysinit file, but when I downloaded your zip file and used your sysinit, it worked, so it must be a problem from my side ;)

Thanks for this, I can finally use "Odex Me" ;)

Great, just wanted to be sure I hadn't made any typos/errors in the guide.
 

Melch1zedeK

Member
Mar 12, 2013
7
2
Thanks, I use your guide and worksperfect for my RK3066 devices. Very simple to understand all steps and what we are doing to our system, perfect for me. Thanks again dude
 

wd5gnr

Senior Member
Dec 20, 2010
460
130
Houston
  • Like
Reactions: moliverac8

wsiegel

Member
Mar 9, 2013
14
1
Question? what is the difference in this method and running a script?

Init.d is how Linux and many Android (which is kind of Linux, after all) systems manage executing commands on boot up.

The /etc/init.d files run in numerical order as root and you can do things like change system settings, manipulate the file system, etc.

See the init.d section linked below for some ideas.

http://xdaforums.com/showthread.php?t=1227269

I use the "swap memory script" and was wondering if it would also work this way with the init.d If so would there be any benefit this way over the current way of running it one way or the other? One drawback I see running the script as is is that I have to wait once the system has fully booted until the script has run and I see the Smanager screen to let me know that my memory has been remounted.

Thanks for the info and the learning process.

Here is the script and the link.
http://xdaforums.com/showthread.php?t=1961097
Code:
sleep 5
mount -o remount,rw /
mount -t vfat -o umask=0000 /dev/block/vold/179:25 /mnt/sdcard
sleep 5
mount -o bind /data/media /mnt/extSdCard
 

wsiegel

Member
Mar 9, 2013
14
1
External memory wasn't ready

As long as the device is ready to mount at boot time and doesn't get remounted, ought to work. Backup and try it ;)

Thanks for the guide, but I think that the external memory was not ready to be mounted at that time. it didn't see the card till after boot. It was worth a shot, Reverted back to the script in /data and all worked again,

Note: I didn't find /system/xbin/run-parts however, I did find /system/bin/run-parts and changed the path to reflect that, I don't think this was an issue but I'm not 100% sure.
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 8
    I have been resisting the urge to flash a custom ROM for a bit, but I really miss having init.d support. So I read a few threads for other phones and rolled my own.

    Warnings
    I borrowed bits and pieces from various places. If you don't know what init.d is, you probably don't want to do this. If you aren't willing to take responsibility for bricking your tablet, don't do this. Seriously, the risk of bricking is very low, but if you aren't comfortable booting into an adb shell from recovery, maybe this is not for you. Strongly suggest a nandroid backup before you get started so if you totally bork things you can just hit rewind.

    Note: The latest CWM may prompt you on a reboot that the ROM may overwrite the bootloader and offer to fix it for you. Don't do that. The init.d hack takes over the bootloader install script, but does not change your bootloader! If you accidentally do let it fix things for you, just rebuild the install-bootloader.sh file. The other steps should be fine.

    Prerequisites
    First, you need root, busybox, and some sort of terminal (either adb, or some terminal you like using on the tablet).
    I have found that I like Busybox Installer (from the market; https://play.google.com/store/apps/details?id=com.jrummy.busybox.installer) but for some reason it doesn't create new symlinks unless you click advanced install.

    Let's get to it!
    In the shell (don't type # or anything after #):

    Code:
    su       # get root
    mount -o remount,rw /system     # get access to /system (4.04 seems to mount ro as is usual; seems like the original mounted rw)
    which run-parts     # if you don't see /system/xbin/run-parts you need to install/reinstall busybox; if it is somewhere else, note it
    mkdir /system/etc/init.d

    Create a file called sysinit -- we are going to put it in /system/bin. You can edit it in place with vi, mount your tablet and edit it on your computer, or create it on the computer and push it via adb. Whatever.

    Here's the file (you do need the # and the things after it in the file!):
    Code:
    #!/system/bin/sh
    export PATH=/sbin:/system/sbin:/system/bin:/system/xbin
    /system/bin/logwrapper /system/xbin/run-parts /system/etc/init.d

    Note that if your run-parts is not in /system/xbin (from the which command) then fix the above to reflect your reality.

    In the shell, make it executable
    Code:
    chmod 755 /system/bin/sysinit

    Now go in the init.d directory and create some things you want to run at start up. For example:
    Code:
    cd /system/etc/init.d
    echo '#!/system/bin/sh' >99test    # note: you do need the first # in this line but not the 2nd!
    echo 'date >>/data/tmp/init.d-log.txt' >>99test
    chmod 755 99test

    Here's a more practical one (yes, you need the # signs). Name it something like 10diskperf -- don't forget to chmod it.
    Code:
    #!/system/bin/sh
    # Set disk read aheads to 1024
    chmod 777 /sys/block/mmcblk0/queue/read_ahead_kb
    echo "1024" > /sys/block/mmcblk0/queue/read_ahead_kb
    chmod 777 /sys/block/mmcblk1/queue/read_ahead_kb
    echo "1024" > /sys/block/mmcblk1/queue/read_ahead_kb
    chmod 777 /sys/devices/virtual/bdi/179:0/read_ahead_kb
    echo "1024" > /sys/devices/virtual/bdi/179:0/read_ahead_kb

    Or here is one to tweak some TCP parameters (25sysctl):
    Code:
    #!/system/bin/sh
    sysctl -w net.core.rmem_max=524288
    sysctl -w net.core.wmem_max=524288
    sysctl -w net.ipv4.tcp_rmem=6144 87380 524288
    sysctl -w net.ipv4.tcp_wmem=6144 87380 524288

    Whatever files you put in, you need to remember to make them executable (chmod 755).

    Finally, you need to kick it all off at start up. The hack for that is we are going to create /system/etc/install-recovery.sh which apparently runs on each boot.


    Code:
    cd /system/etc
    echo '#!/system/bin/sh' >install-recovery.sh
    echo '/system/bin/sysinit' >>install-recovery.sh
    chmod 755 install-recovery.sh

    Tips and troubleshooting

    If you are too lazy to cut and paste I have the files here (View attachment init.d-support.zip) that you can just move to the right places and change permission. If you are really lazy there is lightly tested install script below.

    I like to try running the whole thing before a reboot to see if I get any errors:

    Code:
    /system/etc/install-recovery.sh

    I'd suggest putting the 99test file in first. Verify that you get the expected file in /data/tmp and then reboot and check again. Then you can remove 99test.

    Same goes for adding new scripts. Try running them from the shell to see if they throw errors before you reboot!

    If you have trouble, see if this looks right:
    Code:
    ls -ld /system/etc/install-recovery.sh /system/bin/sysinit /system/etc/init.d /system/xbin/run-parts
    -rwxr-xr-x root     root           39 2012-07-14 10:00 install-recovery.sh
    -rwxr-xr-x root     root          140 2012-07-14 10:01 sysinit
    drwxrwxrwx root     root              2012-07-14 10:10 init.d
    lrwxrwxrwx root     root              2012-07-14 09:55 run-parts -> /system/xbin/busybox

    For the brave
    The install-init.d zip file (View attachment install-init.d.zip) contains a lightly tested script that SHOULD do the install steps for you.
    Send the file to your android to someplace that can execute code (e.g., /system/xbin; I had to use adb to put it on the sdcard and then move it to /systemxbin in the shell since I don't have the adb root kernel installed).
    Code:
    cd /system/xbin     # or wherever you have it
    chmod 755 install-init.d
    ./install-init.d

    It performs rude checks to see if init.d exists, and tries to handle moving or missing busybox. It only installs 99test as a script.

    Let me know if this works or doesn't work for you.

    For the extra brave: There is no reason this should only work on the Samsung. This ought to work on pretty much most stock ROMs as long as they execute install-recovery.sh on start up.


    Scripts

    What do you put in your init.d? If you post anything cool I'll put it up here in the op.

    • One that gave me some real gains in I/O performance required a new version of the tune2fs executable. By default, it is part of busybox but the busybox one only has a few simple options. I've included a stand alone version and the script 10disktune here View attachment disktune.zip. Unpack the zip and put the 10disktune in /system/etc/init.d (don't forget to chmod) and put tune2fs in /system/bin (chmod that too). Note that busybox has one in /system/xbin but the script specifically calls out the one in /system/bin.
    • Here's one that will zipalign your apks on each boot
      Code:
      #!/system/bin/sh
      for apk in /data/app/*.apk ; do
        zipalign -c 4 $apk
        ZCHECK=$?
        if [ $ZCHECK -eq 1 ]; then
          zipalign -f 4 $apk /cache/$(basename $apk)
          if [ -e /cache/$(basename $apk) ]; then
            cp -p -f /cache/$(basename $apk) $apk
            rm /cache/$(basename $apk)
          fi;
        fi;
      done;



    Fin

    Corrections welcome. I considered using exec or . to load some of this into one shell but given that it runs once at startup, I figured it is fine as is.

    All files for reference

    1
    What is thhe utility of this?

    Init.d is how Linux and many Android (which is kind of Linux, after all) systems manage executing commands on boot up.

    The /etc/init.d files run in numerical order as root and you can do things like change system settings, manipulate the file system, etc.

    See the init.d section linked below for some ideas.

    http://xdaforums.com/showthread.php?t=1227269
    1
    As long as the device is ready to mount at boot time and doesn't get remounted, ought to work. Backup and try it ;)