aboutsummaryrefslogtreecommitdiffstats
path: root/docs/init-scripts.tex
blob: c8b07500b39e975e9dd499e9337d8f56712d366a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Because OpenWrt uses its own init script system, all init scripts must be installed
as \texttt{/etc/init.d/\textit{name}} use \texttt{/etc/rc.common} as a wrapper.

Example: \texttt{/etc/init.d/httpd}

\begin{Verbatim}
#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

START=50
start() {
    [ -d /www ] && httpd -p 80 -h /www -r OpenWrt
}

stop() {
    killall httpd
}
\end{Verbatim}

as you can see, the script does not actually parse the command line arguments itself.
This is done by the wrapper script \texttt{/etc/rc.common}.

\texttt{start()} and \texttt{stop()} are the basic functions, which almost any init
script should provide. \texttt{start()} is called when the user runs \texttt{/etc/init.d/httpd start}
or (if the script is enabled and does not override this behavior) at system boot time.

Enabling and disabling init scripts is done by running \texttt{/etc/init.d/\textit{name} enable}
or \texttt{/etc/init.d/\textit{name} disable}. This creates or removes symbolic links to the
init script in \texttt{/etc/rc.d}, which is processed by \texttt{/etc/init.d/rcS} at boot time.

The order in which these scripts are run is defined in the variable \texttt{START} in the init
script. Changing it requires running \texttt{/etc/init.d/\textit{name} enable} again.

You can also override these standard init script functions:
\begin{itemize}
    \item \texttt{boot()} \\
        Commands to be run at boot time. Defaults to \texttt{start()}

    \item \texttt{restart()} \\
        Restart your service. Defaults to \texttt{stop(); start()}

    \item \texttt{reload()} \\
        Reload the configuration files for your service. Defaults to \texttt{restart()}

\end{itemize}

You can also add custom commands by creating the appropriate functions and referencing them
in the \texttt{EXTRA\_COMMANDS} variable. Helptext is added in \texttt{EXTRA\_HELP}.

Example:

\begin{Verbatim}
status() {
    # print the status info
}

EXTRA_COMMANDS="status"
EXTRA_HELP="        status  Print the status of the service"
\end{Verbatim}
pan> useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # use strict; use Getopt::Std; use File::stat; my $version = "0.1"; my %arg = ( o => 'redboot.script', s => 0x1000, f => 0xbe430000, a => 0x80010000, l => 0x7c0000, t => 20, ); my $prog = $0; $prog =~ s/^.*\///; getopts("r:k:o:s:f:a:l:t:vh", \%arg); die "usage: $prog ~opts~ -r <file> : input rootfs file -k <file> : input kernel file -o <file> : output image file, default $arg{o} -s <size_kb> : redboot script size, default ".sprintf('%d', parse_num($arg{s}))." -f <baseaddr> : flash base, default ".sprintf('0x%x', parse_num($arg{f}))." -a <loadaddr> : Kernel load address, default ".sprintf('0x%x', parse_num($arg{a}))." -l <linux_kb> : linux partition size, default ".sprintf('0x%x', parse_num($arg{l}))." -t <timeout> : redboot script timeout, default ".sprintf('%d', parse_num($arg{t}))." -v : be more verbose -h : help, version $version EXAMPLES: $prog -k kern -r rootfs " if $arg{h} || !$arg{k} || !$arg{r}; sub parse_num { my $num = @_[0]; if (index(lc($num), lc("0x")) == 0) { return hex($num); } else { return $num + 0; } } sub gen_script { my $kernel_off = parse_num($arg{s}); my $kernel_addr = parse_num($arg{f}); my $kernel_len = stat($arg{k})->size; my $rootfs_off = $kernel_off + $kernel_len; my $rootfs_addr = $kernel_addr + $kernel_len; my $rootfs_len = parse_num($arg{l}) - $kernel_len; my $rootfs_size = stat($arg{r})->size; my $load_addr = parse_num($arg{a}); my $timeout = parse_num($arg{t}); if ($arg{v}) { printf "kernel_off: 0x%x(%u)\n", $kernel_off, $kernel_off; printf "kernel_addr: 0x%x(%u)\n", $kernel_addr, $kernel_addr; printf "kernel_len: 0x%x(%u)\n", $kernel_len, $kernel_len; printf "rootfs_off: 0x%x(%u)\n", $rootfs_off, $rootfs_off; printf "rootfs_addr: 0x%x(%u)\n", $rootfs_addr, $rootfs_addr; printf "rootfs_len: 0x%x(%u)\n", $rootfs_len, $rootfs_len; printf "rootfs_size: 0x%x(%u)\n", $rootfs_size, $rootfs_size; } open(FO, ">$arg{o}"); printf FO "fis init -f\n"; printf FO "\n"; printf FO "fconfig boot_script true\n"; printf FO "fconfig boot_script_data\n"; printf FO "fis load -b 0x%x -d kernel\n", $load_addr; printf FO "exec -c \"noinitrd\" 0x%x\n", $load_addr; printf FO "\n"; printf FO "fconfig boot_script_timeout %d\n", $timeout; printf FO "\n"; printf FO "fis create -o 0x%x -f 0x%x -l 0x%x kernel\n", $kernel_off, $kernel_addr, $kernel_len; printf FO "\n"; printf FO "fis create -o 0x%x -s 0x%x -f 0x%x -l 0x%x rootfs\n", $rootfs_off, $rootfs_size, $rootfs_addr, $rootfs_len; printf FO "\n"; printf FO "reset\n"; close FO; } # MAIN gen_script();