Changes between Initial Version and Version 1 of OpenWrt/init


Ignore:
Timestamp:
10/22/2017 05:28:45 AM (2 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OpenWrt/init

    v1 v1  
     1[[PageOutline]]
     2
     3= OpenWrt Init System =
     4The OpenWrt Linux Distro uses an init system similar to sysvinit common on most Linux Distros.
     5
     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
     17
     18For more info see OpenWrt documentation:
     19 * http://wiki.openwrt.org/doc/techref/process.boot
     20 * http://wiki.openwrt.org/doc/techref/requirements.boot.process
     21
     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)
     24
     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*'
     27
     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:
     29{{{
     30exec 1>/dev/console  ;# redirect stdout to serial console
     31exec 2>/dev/console  ;# redirect stderr to serial console
     32}}}
     33
     34
     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.
     36
     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.
     38
     39== Example ==
     40
     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.
     42
     43The key once creating this script is to then enable it by typing
     44{{{
     45/etc/init.d/gst-httpd-watchdog enable
     46}}}
     47This will place a symlink in /etc/rc.d/ as noted below
     48{{{
     49S55gst-httpd-watchdog
     50}}}
     51
     52Here is the actual code /etc/init.d/ryantest
     53{{{
     54root@OpenWrt:/etc/init.d# cat ryantest
     55#!/bin/sh /etc/rc.common
     56
     57NAME=gst-httpd-watchdog
     58START=55
     59STOP=55
     60
     61watchdog()
     62{
     63ps -aef | grep gst | grep -v grep
     64
     65while [ 1 ]
     66do
     67
     68proc=$(ps -aef | grep gst | grep -v grep )
     69
     70echo 'proc='$proc
     71
     72if [ -z "$proc" ]
     73then
     74{
     75        echo 'gst process is not found, now restarting'
     76        /etc/init.d/gst-httpd restart
     77}
     78else
     79        echo 'gst process is running'
     80fi
     81sleep 10s;
     82
     83done
     84
     85}
     86
     87start()
     88{
     89( watchdog ) &
     90}
     91}}}