-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit
256 lines (209 loc) · 6.69 KB
/
init
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
# Some sanity checks
if [[ $$ -ne 1 ]]; then
echo >&2 "This script is an init. Run it in a VM or as an initramfs"
exit 1
fi
# reboot in case of termination
function initExit { echo b >| /proc/sysrq-trigger; }
trap initExit EXIT
typeset MYSELF="$(realpath $0)"
typeset MYPATH="${MYSELF%/*}"
set -o nounset -o noclobber
export LC_ALL=C
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"
export PS4=' (${BASH_SOURCE##*/}:$LINENO ${FUNCNAME[0]:-main}) '
function logDebug { echo >&2 "[DBG] $@"; }
function logInfo { echo >&2 "[INF] $@"; }
function logWarning { echo >&2 "[WRN] $@"; }
function logError { echo >&2 "[ERR] $@"; }
function getVal {
typeset key="$1"
typeset tuple
for tuple in $(< /proc/cmdline); do
# Simple bool
[[ "$tuple" == "$key" ]] && return 0
# With value
if [[ "${tuple%%=*}" == "$key" ]]; then
echo "${tuple#*=}"
return 0
fi
done
return 1
}
# #############################################################################
#
# Minimal system setup
#
# #############################################################################
mount -t proc none /proc
mount -t sysfs none /sys
mount -t tmpfs -o size=1g tmpfs /mnt/ramdisk
# Debug
getVal init.debug && set -x
# #############################################################################
#
# Network configuration
#
# #############################################################################
# Setup network (just "eth*" no udev to rename)
typeset -A nicsName= nicsAddress= nicsCarrier= nicsDuplex= nicsIp4= nicsSpeed=
for nicPath in /sys/class/net/*; do
# Skip virtual NICs
[[ -e "$nicPath/device" ]] || continue
typeset nicName="${nicPath##*/}"
typeset nicId="$(<$nicPath/ifindex)"
typeset nicAddr="$(<$nicPath/address)"
# Up iface to have link status
logInfo "Setting up '$nicName' ($nicAddr)"
ip link set "$nicName" up
# Wait a bit for carrier to up
sleep 1
typeset nicCarrier="$(<$nicPath/carrier)"
typeset nicDuplex="$(<$nicPath/duplex)"
typeset nicSpeed="$(<$nicPath/speed)"
nicsName[$nicId]="$nicName"
nicsAddress[$nicId]="$nicAddr"
nicsCarrier[$nicId]="$nicCarrier"
nicsDuplex[$nicId]="$nicDuplex"
nicsSpeed[$nicId]="$nicSpeed"
# More configuration needed
typeset netCfg="$(getVal "net.$nicId")"
[[ -z "$netCfg" ]] && netCfg="$(getVal "net.$nicName")"
[[ -z "$netCfg" ]] && netCfg="$(getVal "net.$nicAddr")"
[[ -z "$netCfg" ]] && netCfg="$(getVal "net.${nicAddr//:/}")"
[[ -z "$netCfg" ]] && [[ "$nicCarrier" == "1" ]] && netCfg="$(getVal "net.link")"
[[ -z "$netCfg" ]] && netCfg="$(getVal "net.all")"
if [[ -n "$netCfg" ]]; then
case "$netCfg" in
dhcp)
if type -p "dhclient" >/dev/null; then
logInfo "Using dhclient on '$nicName'"
mkdir -p "/var/lib/dhclient"
dhclient -sf /init.d/net.dhclient --timeout 15 $nicName
elif type -p "dhcpcd" >/dev/null; then
logInfo "Using dhcpcd on '$nicName'"
dhcpcd $nicName
else
logError "No dhcp client available for '$nicName'"
fi
;;
fixed:*)
logError "Fixed IP address is not implemented yet"
;;
*)
logError "Unknown parameter value: '$nicCfg'"
;;
esac
# Fetch assigned IP Addresses
while read line; do
ip="${line##*inet }"
ip="${ip%% *}"
set +u
nicsIp4[$nicId]+="$ip "
set -u
done < <(ip -o -f inet addr show dev $nicName)
fi
done
# #############################################################################
#
# Some information
#
# #############################################################################
typeset cpuInfo="$(</proc/cpuinfo)"
typeset cpuCnt="${cpuInfo##*processor}" ; cpuCnt="${cpuCnt%%$'\n'*}"; cpuCnt="${cpuCnt#*: }"
typeset cpuMod="${cpuInfo##*model name}"; cpuMod="${cpuMod%%$'\n'*}"; cpuMod="${cpuMod#*: }"
typeset ramInfo="$(</proc/meminfo)"
typeset ramTot="${ramInfo##*MemTotal:}"; ramTot="${ramTot%% kB*}"; ramTot="${ramTot##* }"
logInfo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
logInfo "# Minimal initrd init"
logInfo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
logInfo "#"
logInfo "# cmdline: $(</proc/cmdline)"
logInfo "# cpu: $((1 + $cpuCnt)) * $cpuMod"
logInfo "# ram: $ramTot"
logInfo "#"
logInfo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
logInfo "#"
# Skip id=0 (loopback)
for ((i=1; i<254; i+=1 )); do
if (set +u; [[ -n "${nicsName[$i]}" ]] ); then
logInfo "# NicId:$i name:${nicsName[$i]} addr:${nicsAddress[$i]} Carrier:${nicsCarrier[$i]} Duplex:${nicsDuplex[$i]} Speed:${nicsSpeed[$i]} IPv4:${nicsIp4[$i]:-}"
fi
done
typeset dmiPath="/sys/class/dmi/id"
if [[ -d "$dmiPath" ]]; then
logInfo "#"
logInfo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
logInfo "#"
for dmiType in chassis product board bios; do
typeset serialPath="$dmiPath/${dmiType}_serial"
typeset vendorPath="$dmiPath/${dmiType}_vendor"
typeset versionPath="$dmiPath/${dmiType}_version"
typeset serial= vendor= version=
[[ -r "$serialPath" ]] && serial="$(<$serialPath)"
[[ -r "$vendorPath" ]] && vendor="$(<$vendorPath)"
[[ -r "$versionPath" ]] && version="$(<$versionPath)"
logInfo "# ${dmiType^} serial: $serial vendor: $vendor version: $version"
done
else
# try dmidecode
#dmidecode
:
fi
logInfo "#"
logInfo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
logInfo
#cat >| "/stress/prime.ini" <<-EOT
# TortureThreads= ; Default is to detect automatically.
# MinTortureFFT=8 ; In K
# MaxTortureFFT=4096 ; In K
# TortureMem=8 ; In MiB. Value is per thread.
# TortureTime=15 ; In minutes
#EOT
#echo "===== Running stress test mprime"
#/stress/mprime -t
# If we need to reexec into something else
typeset initExec="$(getVal "init.exec")"
if [[ -n "$initExec" ]]; then
logInfo "Executing '$initExec' from bootparam"
if [[ -x "$initExec" ]]; then
exec $initExec
else
logError "Not executable, Fallback to /bin/bash"
exec /bin/bash
fi
fi
# Try to use our scripts in lexicographical order
for file in /init.d/startup/*; do
# Execute script
if [[ -x "$file" ]]; then
logInfo "Running startup script '$file'"
"$file"
logInfo " return code: $?"
fi
done
# #############################################################################
#
# Cleanup
#
# #############################################################################
sync
# Umount all blockdevices
typeset -a mntToUmount=()
while read blk mnt type junk; do
if [[ "${blk:0:1}" == "/" ]]; then
mntToUmount+=("$mnt")
fi
done < /proc/mounts
typeset -i i="${#mntToUmount[@]}"
while [[ $i -gt 0 ]]; do
typeset mnt="$mntToUmount[$(($i-1))]"
logInfo "Umounting '$mnt'"
umount "$mnt"
if [[ $? -ne 0 ]]; then
logError "Error during umount of '$mnt'."
fi
i+=-1
done
logInfo "Init ended. Will reboot from sysrq"