aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/initrdscripts/initramfs-framework/init
blob: 9291ad5c219299ffd84ffada3b2ce064ad3e10a9 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh
# Copyright (C) 2011 O.S. Systems Software LTDA.
# Licensed on MIT
#
# Provides the API to be used by the initramfs modules
#
# Modules need to provide the following functions:
#
# <module>_enabled : check if the module ought to run (return 1 to skip)
# <module>_run     : do what is need
#
# Boot parameters are available on environment in the as:
#
# 'foo=value' as 'bootparam_foo=value'
# 'foo' as 'bootparam_foo=true'
# 'foo.bar[=value] as 'foo_bar=[value|true]'

# Register a function to be called before running a module
# The hook is called as:
#   <function> pre <module>
add_module_pre_hook() {
	MODULE_PRE_HOOKS="$MODULE_PRE_HOOKS $1"
}

# Register a function to be called after running a module
# The hook is called as:
#   <function> post <module>
add_module_post_hook() {
	MODULE_POST_HOOKS="$MODULE_POST_HOOKS $1"
}

# Load kernel module
load_kernel_module() {
	if modprobe $1 >/dev/null 2>&1; then
		info "Loaded module $1"
	else
		debug "Failed to load module $1"
	fi
}

# Prints information
msg() {
	echo "$@" >/dev/console
}

# Prints information if verbose bootparam is used
info() {
	[ -n "$bootparam_verbose" ] && echo "$@" >/dev/console
}

# Prints information if debug bootparam is used
debug() {
	[ -n "$bootparam_debug" ] && echo "DEBUG: $@" >/dev/console
}

# Prints a message and start a endless loop
fatal() {
    echo $1 >/dev/console
    echo >/dev/console

    if [ -n "bootparam_init_fatal_sh" ]; then
        sh
    else
	while [ "true" ]; do
		sleep 3600
	done
    fi
}

# Variables shared amoung modules
ROOTFS_DIR="/rootfs" # where to do the switch root
MODULE_PRE_HOOKS=""  # functions to call before running each module
MODULE_POST_HOOKS="" # functions to call after running each module
MODULES_DIR=/init.d  # place to look for modules

# make mount stop complaining about missing /etc/fstab
touch /etc/fstab

# initialize /proc, /sys and /var/lock
mkdir -p /proc /sys /var/lock
mount -t proc proc /proc
mount -t sysfs sysfs /sys

# populate bootparam environment
for p in `cat /proc/cmdline`; do
	opt=`echo $p | cut -d'=' -f1`
	opt=`echo $opt | tr '.-' '__'`
	if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
		eval "bootparam_${opt}=true"
	else
		value="`echo $p | cut -d'=' -f2-`"
		eval "bootparam_${opt}=\"${value}\""
	fi
done

# use /dev with devtmpfs
if grep -q devtmpfs /proc/filesystems; then
	mkdir -p /dev
	mount -t devtmpfs devtmpfs /dev
else
	if [ ! -d /dev ]; then
		fatal "ERROR: /dev doesn't exist and kernel doesn't has devtmpfs enabled."
	fi
fi

mkdir $ROOTFS_DIR

# Load and run modules
for m in $MODULES_DIR/*; do
	# Skip backup files
	if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then
		continue
	fi

	module=`basename $m | cut -d'-' -f 2`
	debug "Loading module $module"

	# pre hooks
	for h in $MODULE_PRE_HOOKS; do
		debug "Calling module hook (pre): $h"
		eval "$h pre $module"
		debug "Finished module hook (pre): $h"
	done

	# process module
	. $m

	if ! eval "${module}_enabled"; then
		debug "Skipping module $module"
		continue
	fi

	debug "Running ${module}_run"
	eval "${module}_run"

	# post hooks
	for h in $MODULE_POST_HOOKS; do
		debug "Calling module hook (post): $h"
		eval "$h post $module"
		debug "Finished module hook (post): $h"
	done
done

# Catch all
fatal "ERROR: Initramfs failed to initialize the system."