summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/initrdscripts/initramfs-framework/init
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-core/initrdscripts/initramfs-framework/init')
-rwxr-xr-xmeta/recipes-core/initrdscripts/initramfs-framework/init136
1 files changed, 136 insertions, 0 deletions
diff --git a/meta/recipes-core/initrdscripts/initramfs-framework/init b/meta/recipes-core/initrdscripts/initramfs-framework/init
new file mode 100755
index 0000000000..fc4b0db4b4
--- /dev/null
+++ b/meta/recipes-core/initrdscripts/initramfs-framework/init
@@ -0,0 +1,136 @@
1#!/bin/sh
2# Copyright (C) 2011 O.S. Systems Software LTDA.
3# Licensed on MIT
4#
5# Provides the API to be used by the initramfs modules
6#
7# Modules need to provide the following functions:
8#
9# <module>_enabled : check if the module ought to run (return 1 to skip)
10# <module>_run : do what is need
11#
12# Boot parameters are available on environment in the as:
13#
14# 'foo=value' as 'bootparam_foo=value'
15# 'foo' as 'bootparam_foo=true'
16
17# Register a function to be called before running a module
18# The hook is called as:
19# <function> pre <module>
20add_module_pre_hook() {
21 MODULE_PRE_HOOKS="$MODULE_PRE_HOOKS $1"
22}
23
24# Register a function to be called after running a module
25# The hook is called as:
26# <function> post <module>
27add_module_post_hook() {
28 MODULE_POST_HOOKS="$MODULE_POST_HOOKS $1"
29}
30
31# Load kernel module
32load_kernel_module() {
33 if modprobe $1 >/dev/null 2>&1; then
34 info "Loaded module $1"
35 else
36 debug "Failed to load module $1"
37 fi
38}
39
40# Prints information
41msg() {
42 echo "$@" >/dev/console
43}
44
45# Prints information if verbose bootparam is used
46info() {
47 [ -n "$bootparam_verbose" ] && echo "$@" >/dev/console
48}
49
50# Prints information if debug bootparam is used
51debug() {
52 [ -n "$bootparam_debug" ] && echo "DEBUG: $@" >/dev/console
53}
54
55# Prints a message and start a endless loop
56fatal() {
57 echo $1 >/dev/console
58 echo >/dev/console
59
60 while [ "true" ]; do
61 sleep 3600
62 done
63}
64
65# Variables shared amoung modules
66ROOTFS_DIR="/rootfs" # where to do the switch root
67MODULE_PRE_HOOKS="" # functions to call before running each module
68MODULE_POST_HOOKS="" # functions to call after running each module
69MODULES_DIR=/init.d # place to look for modules
70
71# initialize /proc and /sys
72mkdir -p /proc /sys
73mount -t proc proc /proc
74mount -t sysfs sysfs /sys
75
76# populate bootparam environment
77for p in `cat /proc/cmdline`; do
78 opt="${p%%=*}"
79 opt=${opt/-/_}
80 if [ "${p/=/}" = "$p" ]; then
81 eval "bootparam_${opt}=true"
82 else
83 eval "bootparam_${opt}=\"${p#*=}\""
84 fi
85done
86
87# use /dev with devtmpfs
88if grep -q devtmpfs /proc/filesystems; then
89 mkdir -p /dev
90 mount -t devtmpfs devtmpfs /dev
91else
92 if [ ! -d /dev ]; then
93 fatal "ERROR: /dev doesn't exist and kernel doesn't has devtmpfs enabled."
94 fi
95fi
96
97mkdir $ROOTFS_DIR
98
99# Load and run modules
100for m in $MODULES_DIR/*; do
101 # Skip backup files
102 if [ "${m/\~/}" != "$m" ]; then
103 continue
104 fi
105
106 module=`basename $m | cut -d'-' -f 2`
107 debug "Loading module $module"
108
109 # pre hooks
110 for h in $MODULE_PRE_HOOKS; do
111 debug "Calling module hook (pre): $h"
112 eval "$h pre $module"
113 debug "Finished module hook (pre): $h"
114 done
115
116 # process module
117 source $m
118
119 if ! eval "${module}_enabled"; then
120 debug "Skipping module $module"
121 continue
122 fi
123
124 debug "Running ${module}_run"
125 eval "${module}_run"
126
127 # post hooks
128 for h in $MODULE_POST_HOOKS; do
129 debug "Calling module hook (post): $h"
130 eval "$h post $module"
131 debug "Finished module hook (post): $h"
132 done
133done
134
135# Catch all
136fatal "ERROR: Initramfs failed to initialize the system."