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