[OnePlus 6T  ROMs, Kernels, Recoveries, & Other Dev] foobar66:
This thread will allow you to set up init.d (and services.d) scripts on your phone so they can run at boot time.

Prerequisites

- understanding of what "init.d" scripts mean ...

- your phone must be rooted (see https://www.xda-developers.com/oneplus-6t-unlock-bootloader-root/)

- you must have a working Magisk (see https://xdaforums.com/apps/magisk)

- linux knowledge (I am not a Windows guy but instructions below should easily apply to Windows / PowerShell)

- adb knowledge

If this scares you, stop reading and go play with something else.

Attached zip file contains

- a magisk.img file

- a directory with init.d scripts

- a directory with services.d scripts

You can use the scripts provided or not use (some) of them, or write your own.

What's the difference between init.d and services.d scripts?

The idea is to have 2 directories on your phone with scripts:

/system/etc/init.d/
/system/etc/services.d/

The init.d scripts are run early in the boot (when Magisk initializes). The scripts in /system/etc/services.d/ will run a bit "later", to be precise: when sys.boot_completed = true.

Important warrning: even when sys.boot_completed = true, this does NOT guarantee that /sdcard is mounted. Your script can "sleep" until /sdcard is mounted if it relies on things on /sdcard. See for example the code in /system/etc/services.d/LS99maxvolumewarning which will show how you can do that.

Scripts in init.d should NOT rely on any of the file systems being mounted !

The framework will run all scripts in parallel. So be careful that you do not write scripts which depend on eachother!

The framework will run all scripts as background processes so that they do not hinder the normal boot of your phone.

Prepare the basic setup

To use the scripts (or your own), you must first create the directories init.d and services.d; to do that open a linux shell and do:

> adb shell
$ su
# mount -o rw,remount /system
# mkdir /system/etc/init.d/
# mkdir /system/etc/services.d/
# chown 0.0 /system/etc/init.d
# chown 0.0 /system/etc/services.d
# chmod 755 /system/etc/init.d
# chmod 755 /system/etc/services.d
# sync; exit

Putting the scripts on your phone

Download the attached zip file (initd.zip); create a directory in your linux file system and unzip, e.g.

> mkdir mydir
> cd mydir
> unzip ~/initd.zip
> adb push init.d/ /sdcard/
> adb push services.d/ /sdcard/
> adb shell
$ su
# mount -o rw,remount /system
# mv /sdcard/LS00* /system/etc/init.d/
# mv /sdcard/LS99* /system/etc/services.d/
# chown 0.0 /system/etc/init.d/*
# chown 0.0 /system/etc/services.d/*
# chmod 755 /system/etc/init.d/*
# chmod 755 /system/etc/services.d/*
# sync
# exit
$ exit

Installing the magisk image on your phone

First important remark: I need to turn this really into a proper magisk "module" but I need to study that first. Open a linux shell and do:

> cd mydir
> gunzip magisk.img.gz
> adb push magisk.img /sdcard/
> adb shell
$ su
# cd /data/adb
# mv magisk.img magisk.img.orig
# cp /sdcard/magisk.img .
# chown 0.0 magisk.img
# chmod 644 magisk.img
# sync
# exit
$ exit

That's all !!! If you now reboot your phone your init.d and services.d scripts will run.

How can I tell this is working?

Each script has a log file in /data/; whose name is LS00 (for init.d) or LS99 (for services.d) appended with the name of the script. That log file is passed as "$1" into the script and the script code can write to this log file using:

LOGFILE=$1
echo "Hi I am writing to the log" | tee -a $LOGFILE

To check that the log files are there, open a linux shell and do:

> adb shell
$ su
# ls /data/LS*

And you should see something like:

16 /data/LS00blockdev 4 /data/LS99bootclean 4 /data/LS99maxvolumewarning 4 /data/LS99sysctl
4 /data/LS00governors 4 /data/LS99callrecording 4 /data/LS99network 4 /data/LS99trimcaches
4 /data/LS00kerneltweaks 4 /data/LS99cputweaks 4 /data/LS99overlays 4 /data/LS99turnoffnightmode
4 /data/LS00procgate 4 /data/LS99enablecallrecording 4 /data/LS99remounts 4 /data/LS99workqueue
4 /data/LS00readahead 4 /data/LS99hdparm 4 /data/LS99resetprop
4 /data/LS00resetprop 4 /data/LS99magiskhide 4 /data/LS99sqlite

To check the contents of the log files, do:

> adb shell
$ su
# cat /data/LS*

And you will see logging info:

>> Starting /system/etc/init.d/LS00procgate at 19700110-17:21:12
-- remounting: mount -o remount,hidepid=2,gid=3009 /proc
<< Ending /system/etc/init.d/LS00procgate at 19700110-17:21:12
>> Starting /system/etc/services.d/LS99maxvolumewarning at 20181201-07:34:00
-- slept for 4 seconds waiting for /sdcard/Android
-- disabling max volume warning
<< Ending /system/etc/services.d/LS99maxvolumewarning at 20181201-07:33:58

What's next

Write your own scripts (and share them). Note that scripts must be owned by root (chown 0.0) and have 755 linux permissions (chmod) to run.

How does it really work?

No secrets ... magisk.img is actually a magisk module which runs the scripts. To see the inner details, do the following after you have installed the magisk image and rebooted your phone:

> adb shell
$ su
# ls -l /sbin/.core/img/template/
total 12
0 -rw-r--r-- 1 root root 0 2018-03-12 21:19 auto_mount
4 -rw-r--r-- 1 root root 935 2018-08-06 17:59 post-fs-data.sh
4 -rwxr-xr-x 1 root root 498 2018-08-05 10:11 scriptwrapper*
4 -rw-r--r-- 1 root root 2750 2018-08-11 12:07 service.sh

Magisk will run the post-fs-data.sh first and service.sh later. Check the code of both of these files to understand how init.d and services.d are ran (using run-parts). If you want more details please read: https://topjohnwu.github.io/Magisk/guides.html#scripts.

Disable ALL scripts from running

If you want to disable any script from running do:

> adb shell
$ su
# touch /data/noinitrd

To undo this and get your scripts running again, do:

> adb shell
$ su
# rm /data/noinitrd

What do my init.d scripts do?

LS00blockdev: change properties of block devices (non rotational, no kernel io stats, ...)
LS00governors: set all CPU governers (to schedutil; which is actually the 6T default)
LS00kerneltweaks: a few basic kernel tweaks + stop debug of kernel modules
LS00procgate: protections against the procgate security vulnerability (thanks to @topjohnwu)
LS00readahead: change the readahead amount on logical disk devicesw
LS00resetprop: reset model, brand, manufacturer (only useful if you would want to get your phone appear externally as a Pixel; check the code)

What do my services.d scripts do?

LS99bootclean: clean junk and log files
LS99cputweaks: improve scaling governor
LS99enablecallrecording: enable call recording (must be done at every device boot)
LS99execonce: a whole series of settings; this is only executed ONCE
LS99hdparm: increase readahead on /system and /data
LS99magiskhide: hide some packages from seeing root
LS99maxvolumewarning: remove the high volume warning (I am not sure this will always work !!!)
LS99network: TCP transmit queue and congestion control
LS99overlays: enable all overlays automatically (if you use substratum then no need to enable them manually)
LS99remounts: improve file system performance of multiple partitions
LS99resetprop: increase memory used by dalvik
LS99sqlite: REINDEX and VACCUM sqlite database files (the script only runs every 3rd day)
LS99sysctl: optimize linux kernel settings and TCP/IP performance
LS99trimcaches: trim android cache files
LS99turnoffnightmode: reset the night mode to OFF (night mode conflicts with dark mode in newer Google apps)
LS99workqueue: tune kernel work queue