Device configuration with emteria.OS style init.d scripts

Article overview

Introduction
Structure of emteria.OS style init.d scripts
Scripts order and execution
emteria.OS style init.d script example

Introduction

Since Linux is the base for every Android-based operating system it also is the base for emteria.OS. emteria.OS provides an easy way to execute Linux shell scripts while booting. The script execution can be done in different Android boot phases. This is useful, for example, to configure access rights to the GPIO Pins of the Raspberry Pi for your Android application.
The scripts should be placed under:

/data/init.d/

Within the emteria.OS style init.d scripts all Linux shell commands are available to the user.

Examples:

mkdir 

chown

chgrp

chmod

Note: Make sure your scripts have the right Linux access permissions, meaning that they are executable.

Structure of emteria.OS style init.d scripts

Every init.d scripts starts with: 

#!/system/bin/sh

Now an emteria.OS style header follows. The header format follows:

### BEGIN INIT INFO
# Exec: <trigger name>
### END INIT INFO

<trigger name> should be replaced by the keyword matching the Android boot phase where your script should be executed. Currently, the following Android triggers are supported:

Boot phase Trigger name
early-boot early
boot boot
zygote running zygote
boot completed ready

Scripts order and execution

All init.d scripts are executed one by one in a blocking way. This means that you should only perform finite tasks in them, otherwise you are blocking the execution of the subsequent scripts. If you need to perform a recurring task in an endless loop, create one script that calls your other scripts in a non-blocking way.
Note: Read more about this in the Linux man page on Nohup.

Additionally, an order of execution is not guaranteed, therefore, if you need a certain order you need to create one script that calls your other scripts in that order.

emteria.OS style init.d script example

Here is a short example of how to use the emteria.OS style init.d script to configure the GPIO Pins of the Raspberry Pi. We connect an LED to GPIO PIN 5 (PIN 29) and control it through the terminal app (UID 10085). The user can get the UID of the terminal app by opening the terminal and use the following command

id
#!/system/bin/sh

### BEGIN INIT INFO
# Exec: boot
### END INIT INFO

# make GPIO PIN 5 accessible
echo 5 > /sys/class/gpio/export

# wait for creation of folder /sys/class/gpio/gpio5
sleep 1

# configure GPIO PIN 5 as output
echo out > /sys/class/gpio/gpio5/direction

# set value of GPIO PIN 5 to 0
echo 0 > /sys/class/gpio/gpio5/value

# make GPIO PIN 5 accessible for the terminal app (replace the the UID to your app's UID to provide your app access to the GPIO PIN)
chown -R 10085:10085 /sys/class/gpio/gpio5/

After booting with this script the Terminal app is able to access the /sys/class/gpio/gpio5/ so the LED can be controlled by setting /sys/class/gpio/gpio5/value to 1 (LED turned on) or 0 (LED turned off).