Changes between Initial Version and Version 1 of OpenWrt/init

10/22/2017 05:28:45 AM (5 years ago)



  • OpenWrt/init

    v1 v1  
     3= OpenWrt Init System =
     4The OpenWrt Linux Distro uses an init system similar to sysvinit common on most Linux Distros.
     6Here is the boot process in a nutshell:
     7 1. bootloader configures enough low level hardware to load kernel and jump to it with a kernel cmdline
     8 2. kernel inits hardware for everything built 'static' in the kernel
     9 3. kernel mounts the root filesystem (via the root= rootfstype= etc parameters in the kernel cmdline)
     10 4. kernel executes the 'init' process (PID 1).
     11  * this is done in the kernel's init_post function in init/main.c and typically will execute whichever is found first: /sbin/init, /etc/init, /bin/init, /bin/sh
     12  * for OpenWrt this function is patched so that it looks for and executes only /etc/preinit
     13 5. /etc/preinit does some low level work such as mounting filesystems etc
     14 6. /etc/init.d/rcS is run which executes all scripts in /etc/rc.d/S*
     15  * files here are typically symlinks to scripts in /etc/init.d and have a number preceeding the name to dictate a priority affects order of execution
     16  * /etc/init.d/ scripts include /etc/rc.common which implements enable/disable functions which create the symlinks.  The start/stop priority numbers are defined in the init scripts
     18For more info see OpenWrt documentation:
     19 *
     20 *
     22== Failsafe bootup ==
     23The /etc/preinit script has a 'failsafe' mechanism which allows you a few seconds to answer a prompt to go into 'failsafe mode'.  This simply stops execution of the init scripts in case you have a mis-configuration or other issue that is keeping you from configuring the target board.  There are many configurations for the failsafe mechanism at the top of /etc/preinit (network config, timeout, messages, etc)
     25== Adding an init script ==
     26If you want something simple to be done 'near the end of every boot' a good place to put it may be in /etc/rc.local as this is executed by the /etc/init.d/done script which is symlinked to /etc/rc.d/S95done.  Note that this isn't the 'last' init script run but is for sure near the end.  You can see all the startup scripts in order with 'ls /etc/rc.d/S*'
     28Keep in mind that init scripts run with stdout/stderr supressed from the linux serial console.  If you want to disable this supression you can put the following in your init script to redirect stdout/stderr to the serial console:
     30exec 1>/dev/console  ;# redirect stdout to serial console
     31exec 2>/dev/console  ;# redirect stderr to serial console
     35If you want to add a more complex init process that perhaps has a start/stop and a priority you can easily create your own init script by following the examples in /etc/init.d.
     37Note that if using 'sysupgrade' there is a specified set of files that get backed up and restored to the newly upgraded image along with all of UCI.  This is configured in /etc/sysupgrade.conf.  If you want your init changes to persist across a sysupgrade be sure to configure things properly.  You may want to create an OpenWrt package and use UCI configuration to help do that for you.
     39== Example ==
     41Below is an example of a simple script placed on OpenWrt at /etc/init.d/gst-httpd-watchdog.  This script basically checks if a process is running, and if it doesn't find it, then it restarts it.
     43The key once creating this script is to then enable it by typing
     45/etc/init.d/gst-httpd-watchdog enable
     47This will place a symlink in /etc/rc.d/ as noted below
     52Here is the actual code /etc/init.d/ryantest
     54root@OpenWrt:/etc/init.d# cat ryantest
     55#!/bin/sh /etc/rc.common
     63ps -aef | grep gst | grep -v grep
     65while [ 1 ]
     68proc=$(ps -aef | grep gst | grep -v grep )
     70echo 'proc='$proc
     72if [ -z "$proc" ]
     75        echo 'gst process is not found, now restarting'
     76        /etc/init.d/gst-httpd restart
     79        echo 'gst process is running'
     81sleep 10s;
     89( watchdog ) &