]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - IMG/cpio/ventoy/ventoy_chain.sh
update
[Ventoy.git] / IMG / cpio / ventoy / ventoy_chain.sh
1 #!/ventoy/busybox/sh
2 #************************************************************************************
3 # Copyright (c) 2020, longpanda <admin@ventoy.net>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #
18 #************************************************************************************
19
20 ####################################################################
21 # #
22 # Step 1 : Parse kernel parameter #
23 # #
24 ####################################################################
25 if ! [ -e /proc ]; then
26 $BUSYBOX_PATH/mkdir /proc
27 rmproc='Y'
28 fi
29 $BUSYBOX_PATH/mount -t proc proc /proc
30
31 # vtinit=xxx to replace rdinit=xxx
32 vtcmdline=$($CAT /proc/cmdline)
33 for i in $vtcmdline; do
34 if echo $i | $GREP -q vtinit; then
35 user_rdinit=${i#vtinit=}
36 echo "user set user_rdinit=${user_rdinit}" >>$VTLOG
37 fi
38 done
39
40
41 ####################################################################
42 # #
43 # Step 2 : Do OS specific hook #
44 # #
45 ####################################################################
46 ventoy_get_os_type() {
47 echo "kernel version" >> $VTLOG
48 $CAT /proc/version >> $VTLOG
49
50 # rhel5/CentOS5 and all other distributions based on them
51 if $GREP -q 'el5' /proc/version; then
52 echo 'rhel5'; return
53
54 # rhel6/CentOS6 and all other distributions based on them
55 elif $GREP -q 'el6' /proc/version; then
56 echo 'rhel6'; return
57
58 # rhel7/CentOS7/rhel8/CentOS8 and all other distributions based on them
59 elif $GREP -q 'el[78]' /proc/version; then
60 echo 'rhel7'; return
61
62 # Maybe rhel9 rhel1x use the same way? Who knows!
63 elif $EGREP -q 'el9|el1[0-9]' /proc/version; then
64 echo 'rhel7'; return
65
66 # Fedora : do the same process with rhel7
67 elif $GREP -q '\.fc[0-9][0-9]\.' /proc/version; then
68 echo 'rhel7'; return
69
70 # Debian :
71 elif $GREP -q '[Dd]ebian' /proc/version; then
72 echo 'debian'; return
73
74 # Ubuntu : do the same process with debian
75 elif $GREP -q '[Uu]buntu' /proc/version; then
76 echo 'debian'; return
77
78 # Deepin : do the same process with debian
79 elif $GREP -q '[Dd]eepin' /proc/version; then
80 echo 'debian'; return
81
82 # SUSE
83 elif $GREP -q 'SUSE' /proc/version; then
84 echo 'suse'; return
85
86 # ArchLinux
87 elif $EGREP -q 'archlinux|ARCH' /proc/version; then
88 echo 'arch'; return
89
90 # kiosk
91 elif $EGREP -q 'kiosk' /proc/version; then
92 echo 'kiosk'; return
93
94 # gentoo
95 elif $EGREP -q '[Gg]entoo' /proc/version; then
96 echo 'gentoo'; return
97
98 # TinyCore
99 elif $EGREP -q 'tinycore' /proc/version; then
100 echo 'tinycore'; return
101
102 # manjaro
103 elif $EGREP -q 'manjaro|MANJARO' /proc/version; then
104 echo 'manjaro'; return
105
106 # mageia
107 elif $EGREP -q 'mageia' /proc/version; then
108 echo 'mageia'; return
109
110 # pclinux OS
111 elif $GREP -i -q 'PCLinuxOS' /proc/version; then
112 echo 'pclos'; return
113
114 # KaOS
115 elif $GREP -i -q 'kaos' /proc/version; then
116 echo 'kaos'; return
117
118 # Alpine
119 elif $GREP -q 'Alpine' /proc/version; then
120 echo 'alpine'; return
121
122 # NixOS
123 elif $GREP -i -q 'NixOS' /proc/version; then
124 echo 'nixos'; return
125
126 fi
127
128 if [ -e /lib/debian-installer ]; then
129 echo 'debian'; return
130 fi
131
132 if [ -e /etc/os-release ]; then
133 if $GREP -q 'XenServer' /etc/os-release; then
134 echo 'xen'; return
135 elif $GREP -q 'SUSE ' /etc/os-release; then
136 echo 'suse'; return
137 elif $GREP -q 'uruk' /etc/os-release; then
138 echo 'debian'; return
139 elif $GREP -q 'Solus' /etc/os-release; then
140 echo 'rhel7'; return
141 fi
142 fi
143
144 if $BUSYBOX_PATH/dmesg | $GREP -q -m1 "Xen:"; then
145 echo 'xen'; return
146 fi
147
148
149 if [ -e /etc/HOSTNAME ] && $GREP -i -q 'slackware' /etc/HOSTNAME; then
150 echo 'slackware'; return
151 fi
152
153 if [ -e /init ]; then
154 if $GREP -i -q zeroshell /init; then
155 echo 'zeroshell'; return
156 fi
157 fi
158
159 if $EGREP -q 'ALT ' /proc/version; then
160 echo 'alt'; return
161 fi
162
163 if $EGREP -q 'porteus' /proc/version; then
164 echo 'debian'; return
165 fi
166
167 if $GREP -q 'Clear Linux ' /proc/version; then
168 echo 'clear'; return
169 fi
170
171 if $GREP -q 'artix' /proc/version; then
172 echo 'arch'; return
173 fi
174
175 if $GREP -q 'berry ' /proc/version; then
176 echo 'berry'; return
177 fi
178
179 if $GREP -q 'Gobo ' /proc/version; then
180 echo 'gobo'; return
181 fi
182
183 if $GREP -q 'NuTyX' /proc/version; then
184 echo 'nutyx'; return
185 fi
186
187 if [ -d /gnu ]; then
188 vtLineNum=$($FIND /gnu/ -name guix | $BUSYBOX_PATH/wc -l)
189 if [ $vtLineNum -gt 0 ]; then
190 echo 'guix'; return
191 fi
192 fi
193
194 if $GREP -q 'android.x86' /proc/version; then
195 echo 'android'; return
196 fi
197
198 if $GREP -q 'adelielinux' /proc/version; then
199 echo 'adelie'; return
200 fi
201
202 if $GREP -q 'pmagic' /proc/version; then
203 echo 'pmagic'; return
204 fi
205
206 if $GREP -q 'CDlinux' /proc/cmdline; then
207 echo 'cdlinux'; return
208 fi
209
210 if $GREP -q 'parabola' /proc/version; then
211 echo 'parabola'; return
212 fi
213
214 if $GREP -q 'cucumber' /proc/version; then
215 echo 'cucumber'; return
216 fi
217
218 if $GREP -q 'fatdog' /proc/version; then
219 echo 'fatdog'; return
220 fi
221
222 if $GREP -q 'KWORT' /proc/version; then
223 echo 'kwort'; return
224 fi
225
226 if $GREP -q 'iwamoto' /proc/version; then
227 echo 'vine'; return
228 fi
229
230 if $GREP -q 'hyperbola' /proc/cmdline; then
231 echo 'hyperbola'; return
232 fi
233
234 echo "default"
235 }
236
237 VTOS=$(ventoy_get_os_type)
238 echo "OS=###${VTOS}###" >>$VTLOG
239 if [ -e "$VTOY_PATH/hook/$VTOS/ventoy-hook.sh" ]; then
240 $BUSYBOX_PATH/sh "$VTOY_PATH/hook/$VTOS/ventoy-hook.sh"
241 fi
242
243
244 ####################################################################
245 # #
246 # Step 3 : Check for debug break #
247 # #
248 ####################################################################
249 if [ "$VTOY_BREAK_LEVEL" = "03" ] || [ "$VTOY_BREAK_LEVEL" = "13" ]; then
250 $SLEEP 5
251 echo -e "\n\n\033[32m ################################################# \033[0m"
252 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
253 echo -e "\033[32m ################################################# \033[0m \n"
254 if [ "$VTOY_BREAK_LEVEL" = "13" ]; then
255 $CAT $VTOY_PATH/log
256 fi
257 exec $BUSYBOX_PATH/sh
258 fi
259
260
261 ####################################################################
262 # #
263 # Step 4 : Hand over to real init #
264 # #
265 ####################################################################
266 $BUSYBOX_PATH/umount /proc
267 if [ "$rmproc" = "Y" ]; then
268 $BUSYBOX_PATH/rm -rf /proc
269 fi
270
271 if [ -f $VTOY_PATH/ventoy_persistent_map ]; then
272 export PERSISTENT='YES'
273 export PERSISTENCE='true'
274 fi
275
276 cd /
277
278 unset VTLOG FIND GREP EGREP CAT AWK SED SLEEP HEAD
279
280 for vtinit in $user_rdinit /init /sbin/init /linuxrc; do
281 if [ -d /ventoy_rdroot ]; then
282 if [ -e "/ventoy_rdroot$vtinit" ]; then
283 # switch_root will check /init file, this is a cheat code
284 echo 'switch_root' > /init
285 exec $BUSYBOX_PATH/switch_root /ventoy_rdroot "$vtinit"
286 fi
287 else
288 if [ -e "$vtinit" ];then
289 if [ -f "$VTOY_PATH/hook/$VTOS/ventoy-before-init.sh" ]; then
290 $BUSYBOX_PATH/sh "$VTOY_PATH/hook/$VTOS/ventoy-before-init.sh"
291 fi
292 exec "$vtinit"
293 fi
294 fi
295 done
296
297 # Should never reach here
298 echo -e "\n\n\033[31m ############ INIT NOT FOUND ############### \033[0m \n"
299 exec $BUSYBOX_PATH/sh