/
init
290 lines (236 loc) · 6.99 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/sh
################################################################################
# This file is part of OpenELEC - http://www.openelec.tv
# Copyright (C) 2009-2011 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2010-2011 Roman Weber (roman@openelec.tv)
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be 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 OpenELEC.tv; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
################################################################################
UPDATE_DIR=/storage/.update
UPDATE=0
IMAGE_SYSTEM="SYSTEM"
IMAGE_KERNEL="KERNEL"
REBOOT=0
# mount all needed special filesystems
/bin/busybox mount -t devtmpfs none /dev
/bin/busybox mount -t proc none /proc
/bin/busybox mount -t sysfs none /sys
# hide kernel log messages on console
echo '1 4 1 7' > /proc/sys/kernel/printk
# parse command line arguments
while read line; do
for arg in $line; do
case $arg in
debugging)
DEBUG=yes
;;
nosplash)
SPLASH=no
;;
bootchart)
BOOTCHART=yes
;;
fastboot)
FASTBOOT=yes
;;
esac
done
done < /proc/cmdline
if [ "$FASTBOOT" = "yes" ]; then
IONICE="/bin/busybox ionice -c 1 -n 0"
fi
progress() {
if [ "$DEBUG" = "yes" ]; then
echo "### $1 ###"
fi
}
show_splash() {
if [ ! -e /dev/fb0 ]; then
SPLASH=no
fi
if [ -f /flash/oemsplash.png ]; then
SPLASHIMAGE="/flash/oemsplash.png"
elif [ -f /splash/splash.png ]; then
SPLASHIMAGE="/splash/splash.png"
else
SPLASH=no
fi
if [ "$SPLASH" = "no" ]; then
break
else
if [ -f "/sbin/ply-image" ]; then
/sbin/ply-image "$SPLASHIMAGE" &
fi
fi
}
error() {
echo "Error Code: $1 that means: $2"
}
debug_shell() {
echo "### Starting debugging shell... type exit to quit ###"
/bin/busybox sh </dev/tty1 >/dev/tty1 2>&1
}
mount_part() {
# progress "check filesystem $1 ..."
# /sbin/fsck -M -T -a $1 > /dev/null 2>&1
for i in 1 2 3 4 5 6 7 8 9 10; do
ERR_ENV=1
MOUNT_OPTIONS="-o $3 $1 $2"
if [ -n "$4" ]; then
MOUNT_OPTIONS="-t $4 $MOUNT_OPTIONS"
fi
progress "mount filesystem $1 ..."
$IONICE /bin/busybox mount $MOUNT_OPTIONS > /dev/null 2>&1
[ "$?" -eq "0" ] && ERR_ENV=0 && break
/bin/busybox usleep 1000000
done
[ "$ERR_ENV" -ne "0" ] && error "INIT_4" "Could not mount $1" && debug_shell
}
update() {
if [ -f "$UPDATE_DIR/$2" ]; then
echo "updating $1..."
$IONICE /bin/busybox mount -o remount,rw /flash
$IONICE /bin/busybox mv $UPDATE_DIR/$2 $3
$IONICE /bin/busybox mount -o remount,ro /flash
$IONICE /bin/busybox sync
[ "$2" = "$IMAGE_KERNEL" ] && REBOOT=1
fi
}
hfsdiskprep() {
for DEVICE in /dev/sd*; do
for device in $(/bin/busybox blkid $DEVICE); do
case $device in
TYPE=*)
FS_TYPE=${device#TYPE=}
;;
esac
done
if [ "$FS_TYPE" = "\"hfs\"" -o "$FS_TYPE" = "\"hfsplus\"" ]; then
progress "check filesystem $DEVICE [$FS_TYPE]..."
/sbin/fsck_hfs -r -y $DEVICE > /dev/null 2>&1
fi
done
}
mount_boot() {
case $boot in
LABEL=*|UUID=*)
mount_boot_disk
;;
NFS=*)
mount_boot_nfs
;;
NBD=*)
mount_boot_nbd
;;
esac
}
mount_boot_disk() {
# deal with hfs partitions
if [ -x /sbin/fsck_hfs ]; then
hfsdiskprep
fi
mount_part "$boot" "/flash" "ro,noatime"
show_splash
UPDATE=1
mount_disk
if [ -f "/flash/$IMAGE_SYSTEM" ]; then
mount_part "/flash/$IMAGE_SYSTEM" "/sysroot" "ro,loop"
[ "$ERR_ENV" -ne "0" ] && debug_shell
else
error "INIT_2" "Could not find system."
debug_shell
fi
/bin/busybox mount --move /flash /sysroot/flash
/bin/busybox mount --move /storage /sysroot/storage
}
mount_boot_nbd() {
boot=${boot#*=}
retry_nr=0
retry_delay=20
while [ ${retry_nr} -lt ${retry_delay} ] && [ ! -e /sysroot/sbin/init ]; do
[ ${retry_nr} -gt 0 ] && \
$IONICE /bin/busybox nbd-client ${boot%%:*} ${boot##*:} /dev/nbd0 && \
mount_part "/dev/nbd0" "/sysroot" "ro" "squashfs"
retry_nr=$(( ${retry_nr} + 1 ))
[ ! -e /sysroot/sbin/init ] && /bin/busybox usleep 1000000
[ ${retry_nr} -gt 0 ]
done
if [ ! -e /sysroot/sbin/init ]; then
error "INIT_2" "Could not mount NBD root from $boot"
debug_shell
fi
mount_disk
/bin/busybox mount --move /storage /sysroot/storage
}
mount_boot_nfs() {
boot=${boot#*=}
mount_part "$boot" "/flash" "ro,nolock,noatime" "nfs"
show_splash
if [ -f "/flash/$IMAGE_SYSTEM" ]; then
mount_part "/flash/$IMAGE_SYSTEM" "/sysroot" "ro,loop"
[ "$ERR_ENV" -ne "0" ] && debug_shell
else
error "INIT_2" "Could not find system."
debug_shell
fi
mount_disk
/bin/busybox mount --move /flash /sysroot/flash
/bin/busybox mount --move /storage /sysroot/storage
}
mount_disk() {
case $disk in
LABEL=*|UUID=*)
mount_disk_disk
;;
NFS=*)
mount_disk_nfs
;;
esac
}
mount_disk_disk() {
mount_part "$disk" "/storage" "rw,noatime"
if [ $UPDATE -gt 0 ]; then
if [ -f "/flash/MACH_KERNEL" ]; then
IMAGE_KERNEL="MACH_KERNEL"
fi
update "Kernel" "$IMAGE_KERNEL" "/flash/$IMAGE_KERNEL"
update "System" "$IMAGE_SYSTEM" "/flash/$IMAGE_SYSTEM"
if [ $REBOOT -gt 0 ]; then
echo "System reboots now..." && \
/bin/busybox reboot
fi
fi
}
mount_disk_nfs() {
disk=${disk#*=}
# provision NFS overlay
mount_part "$disk" "/storage" "rw,nolock,retrans=10" "nfs"
if [ ! -d /storage/$BOOTIF ]; then
mkdir /storage/$BOOTIF
fi
# remount provisioned NFS overlay
/bin/busybox umount /storage
mount_part "$disk/$BOOTIF" "/storage" "rw,nolock" "nfs"
}
mount_boot
# move some special filesystems
/bin/busybox mount --move /dev /sysroot/dev
/bin/busybox mount --move /proc /sysroot/proc
/bin/busybox mount --move /sys /sysroot/sys
# switch to new sysroot and start real init
exec /bin/busybox switch_root /sysroot /sbin/init
error "INIT_3" "Error in initramfs. Could not switch to new root"
debug_shell