-
Notifications
You must be signed in to change notification settings - Fork 2
/
init
116 lines (108 loc) · 4.29 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
#!/bin/sh
# shellcheck disable=SC2012,SC2039,SC2093,SC1090,SC1091
export PATH="/bin:/sbin:/usr/bin:/usr/sbin"
. init_functions
mount_proc_sys_dev
# forward kernel messages to tty8 to avoid module loading spam etc.
no_fwdcon=$(get_opt no_fwdcon)
[ "$no_fwdcon" != "1" ] && setlogcons 8
silent_boot=$(get_opt silent_boot)
if hook_exists splash; then
# clear potential early kernel logs
printf '\033[H\033[2J' > /dev/tty1
silent_boot=1
else
no_clear=$(get_opt no_clear)
[ "$no_clear" != "1" ] && clear
fi
[ -z "$silent_boot" ] && silent_boot=0
add_const HOST_IP "172.16.42.2"
add_const USB_IP "172.16.42.1"
add_const USB_IFACE "@USB_IFACE@" # e.g. "usb0"
add_const KERNEL_VER "$(uname -r)" # e.g. "5.12.0-oneplus-msm8998"
add_const DEVICE_MODEL "$(cat @DEVICE_MODEL_SYSFS@)" # e.g. "OnePlus 5T"
add_const NPROC $(nproc --all) # e.g. 8
add_const MEM "$(free | awk 'NR==2 {printf "%.2f\n", $2/1024/1024}') GiB" # e.g. "5.49" or "7.46"
init_log "Running kernel $KERNEL_VER on a $DEVICE_MODEL (with $NPROC CPUs & $MEM of RAM)"
# Run all early hooks BEFORE rootfs is mounted
setup_extra_hooks
for hook in $(find /hooks -maxdepth 1 -type f 2>/dev/null | sort -V); do
. "$hook"
done
# RootFS mounting system:
# 1. if rd.rootfs exists, assuming it is a partition containing the rootfs
# 2. if it contains at least 2 '/', assume beginning is partition containing
# either a rootfs image or directory
# 3. if rd.rootfs starts with "nbd:" assume the rest is a rootfs export
# residing on the USB host machine ($HOST_IP)
# 4. if rd.rootfs starts with "super:" assume the rest is a dynpart name in
# the device's super partition
rootfs=$(get_opt rootfs)
@ROOTFS_DEFAULT_INIT@
# Make sure rootfs is defined on kernel cmdline
if [ -z "$rootfs" ]; then
log_kmsg "ERROR: rd.rootfs was not specified! Please add e.g. 'rd.rootfs=/dev/sda13'
to your kernel cmdline OR enable the telnetd hook in initramfs and
investigate manually using your preferred tools such as 'fdisk -l'!" 2
list_pars_and_hang
fi
# Mounting of $rootfs
parse_rootfs_par
if [ $parse_rootfs_par_error -eq 1 ]; then
log_kmsg "ERROR: The specified rootfs '$rootfs' is invalid!" 2
list_pars_and_hang
fi
wait_for_rootfs_par
mount_rootfs
if [ $rootfs_mount_error -gt 0 ]; then
case $rootfs_mount_error in
1) hang "ERROR: Couldn't locate NBD export '$nbd_export' on $HOST_IP!" 2 ;;
2) hang "ERROR: Couldn't mount NBD export '$nbd_export' with block size of $nbd_bs!" 2 ;;
3)
log_kmsg "ERROR: 'super' partition not found!" 2
list_pars_and_hang
;;
4)
log_kmsg "ERROR: Dynpart '$super_dynpart' under super partition not found! Here's your options:" 2
ls /dev/mapper/dynpart-* | cut -c21- | tee -a /init.log /dev/kmsg
hang
;;
5) hang "ERROR: Couldn't mount '$rootfs_par'!" 2 ;;
6) log_kmsg "ERROR: Couldn't locate rootfs image/directory '$rootfs_location' under
'$rootfs_par'! Here's an 'ls -lA':" 2
ls -lA /rootfs_par | tee -a /init.log /dev/kmsg
hang
;;
7) hang "ERROR: Couldn't mount rootfs image '$rootfs_location'!" 2 ;;
esac
fi
init_log "Rootfs type is $rootfs_type"
init="$rootfs_location/sbin/init"
# Workaround for broken /sbin/init absolute symlink as seen on e.g. Droidian
[[ ! -e "$init" && -L "$init" ]] \
&& init="$(readlink_hack "$init" "$rootfs_location")"
# Make sure we've got a valid rootfs with /sbin/init
if [ ! -e "$init" ]; then
log_kmsg "ERROR: Couldn't locate $(real_init) under '$rootfs_location'! Here's an 'ls -lA':" 2
ls -lA "$rootfs_location" | tee -a /init.log /dev/kmsg
hang
fi
# Run all late hooks right BEFORE switching root
for late_hook in $(find /hooks/late -type f 2>/dev/null | sort -V); do
. "$late_hook"
done
# After this point we'll let the rootfs init process take care of things
telnet_pid=$(pgrep telnetd)
[ "$telnet_pid" ] && kill $telnet_pid
total="$(df -Ph | awk 'END {print $2}')"
usage="$(df -P | awk 'END {print $5}')"
os="$(get_os_name "$rootfs_location")"
[ "$os" ] && os=" $os"
init_log "Booting$os from $rootfs_location ($usage of $total used)..."
# log kernel messages to boot console again on non-silent boots
[ "$silent_boot" != "1" ] && setlogcons 0
# silence some distros complaining about a missing /dev/console (e.g. void)
[ ! -e "$rootfs_location"/dev/console ] \
&& mknod "$rootfs_location"/dev/console c 5 1
exec switch_root "$rootfs_location" "$(real_init)"
hang "Failed to switch root :(" 2