]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - IMG/cpio/ventoy/ventoy_chain.sh
support boot Windows VHD(x) at local disk
[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 # Step 2 : Do OS specific hook #
43 # #
44 ####################################################################
45 ventoy_get_os_type() {
46 echo "kernel version" >> $VTLOG
47 $CAT /proc/version >> $VTLOG
48
49 if [ -d /twres ]; then
50 if $GREP -q 'Phoenix' /init; then
51 echo 'phoenixos'; return
52 fi
53 fi
54
55 # rhel5/CentOS5 and all other distributions based on them
56 if $GREP -q 'el5' /proc/version; then
57 echo 'rhel5'; return
58
59 # rhel6/CentOS6 and all other distributions based on them
60 elif $GREP -q 'el6' /proc/version; then
61 echo 'rhel6'; return
62
63 # rhel7/CentOS7/rhel8/CentOS8 and all other distributions based on them
64 elif $GREP -q 'el[78]' /proc/version; then
65 echo 'rhel7'; return
66
67 # Maybe rhel9 rhel1x use the same way? Who knows!
68 elif $EGREP -q 'el9|el1[0-9]' /proc/version; then
69 echo 'rhel7'; return
70
71 # Fedora : do the same process with rhel7
72 elif $GREP -q '\.fc[0-9][0-9]\.' /proc/version; then
73 echo 'rhel7'; return
74
75 # Debian :
76 elif $GREP -q '[Dd]ebian' /proc/version; then
77 echo 'debian'; return
78
79 # Ubuntu : do the same process with debian
80 elif $GREP -q '[Uu]buntu' /proc/version; then
81 echo 'debian'; return
82
83 # Deepin : do the same process with debian
84 elif $GREP -q '[Dd]eepin' /proc/version; then
85 echo 'debian'; return
86
87 # SUSE
88 elif $GREP -q 'SUSE' /proc/version; then
89 echo 'suse'; return
90
91 # ArchLinux
92 elif $EGREP -q 'archlinux|ARCH' /proc/version; then
93 echo 'arch'; return
94
95 # kiosk
96 elif $EGREP -q 'kiosk' /proc/version; then
97 echo 'kiosk'; return
98
99 # gentoo
100 elif $EGREP -q '[Gg]entoo' /proc/version; then
101 if $GREP -q 'daphile' /proc/version; then
102 echo 'daphile'; return
103 fi
104
105 echo 'gentoo'; return
106
107 # TinyCore
108 elif $EGREP -q 'tinycore' /proc/version; then
109 echo 'tinycore'; return
110
111 # manjaro
112 elif $EGREP -q 'manjaro|MANJARO' /proc/version; then
113 echo 'manjaro'; return
114
115 # mageia
116 elif $EGREP -q 'mageia' /proc/version; then
117 echo 'mageia'; return
118
119 # pclinux OS
120 elif $GREP -i -q 'PCLinuxOS' /proc/version; then
121 echo 'pclos'; return
122
123 # KaOS
124 elif $GREP -i -q 'kaos' /proc/version; then
125 echo 'kaos'; return
126
127 # Alpine
128 elif $GREP -q 'Alpine' /proc/version; then
129 echo 'alpine'; return
130
131 elif $GREP -i -q 'PhoenixOS' /proc/version; then
132 echo 'phoenixos'; return
133
134 # NixOS
135 elif $GREP -i -q 'NixOS' /proc/version; then
136 echo 'nixos'; return
137
138
139 fi
140
141 if [ -e /lib/debian-installer ]; then
142 echo 'debian'; return
143 fi
144
145 if [ -e /etc/os-release ]; then
146 if $GREP -q 'XenServer' /etc/os-release; then
147 echo 'xen'; return
148 elif $GREP -q 'SUSE ' /etc/os-release; then
149 echo 'suse'; return
150 elif $GREP -q 'uruk' /etc/os-release; then
151 echo 'debian'; return
152 elif $GREP -q 'Solus' /etc/os-release; then
153 echo 'rhel7'; return
154 elif $GREP -q 'openEuler' /etc/os-release; then
155 echo 'openEuler'; return
156 elif $GREP -q 'fuyu' /etc/os-release; then
157 echo 'openEuler'; return
158 fi
159 fi
160
161 if $BUSYBOX_PATH/dmesg | $GREP -q -m1 "Xen:"; then
162 echo 'xen'; return
163 fi
164
165
166 if [ -e /etc/HOSTNAME ] && $GREP -i -q 'slackware' /etc/HOSTNAME; then
167 echo 'slackware'; return
168 fi
169
170 if [ -e /init ]; then
171 if $GREP -i -q zeroshell /init; then
172 echo 'zeroshell'; return
173 fi
174 fi
175
176 if $EGREP -q 'ALT ' /proc/version; then
177 echo 'alt'; return
178 fi
179
180 if $EGREP -q 'porteus' /proc/version; then
181 echo 'debian'; return
182 fi
183
184 if $GREP -q 'Clear Linux ' /proc/version; then
185 echo 'clear'; return
186 fi
187
188 if $GREP -q 'artix' /proc/version; then
189 echo 'arch'; return
190 fi
191
192 if $GREP -q 'berry ' /proc/version; then
193 echo 'berry'; return
194 fi
195
196 if $GREP -q 'Gobo ' /proc/version; then
197 echo 'gobo'; return
198 fi
199
200 if $GREP -q 'NuTyX' /proc/version; then
201 echo 'nutyx'; return
202 fi
203
204 if [ -d /gnu ]; then
205 vtLineNum=$($FIND /gnu/ -name guix | $BUSYBOX_PATH/wc -l)
206 if [ $vtLineNum -gt 0 ]; then
207 echo 'guix'; return
208 fi
209 fi
210
211 if $GREP -q 'android.x86' /proc/version; then
212 echo 'android'; return
213 fi
214
215 if $GREP -q 'adelielinux' /proc/version; then
216 echo 'adelie'; return
217 fi
218
219 if $GREP -q 'pmagic' /proc/version; then
220 echo 'pmagic'; return
221 fi
222
223 if $GREP -q 'CDlinux' /proc/cmdline; then
224 echo 'cdlinux'; return
225 fi
226
227 if $GREP -q 'parabola' /proc/version; then
228 echo 'parabola'; return
229 fi
230
231 if $GREP -q 'cucumber' /proc/version; then
232 echo 'cucumber'; return
233 fi
234
235 if $GREP -q 'fatdog' /proc/version; then
236 echo 'fatdog'; return
237 fi
238
239 if $GREP -q 'KWORT' /proc/version; then
240 echo 'kwort'; return
241 fi
242
243 if $GREP -q 'iwamoto' /proc/version; then
244 echo 'vine'; return
245 fi
246
247 if $GREP -q 'hyperbola' /proc/cmdline; then
248 echo 'hyperbola'; return
249 fi
250
251 if $GREP -q 'CRUX' /proc/version; then
252 echo 'crux'; return
253 fi
254
255 if [ -f /init ]; then
256 if $GREP -q 'AryaLinux' /init; then
257 echo 'aryalinux'; return
258 elif $GREP -q 'Dragora' /init; then
259 echo 'dragora'; return
260
261 fi
262 fi
263
264 if $GREP -q 'slackware' /proc/version; then
265 echo 'slackware'; return
266 fi
267
268 if $BUSYBOX_PATH/hostname | $GREP -q 'smoothwall'; then
269 echo 'smoothwall'; return
270 fi
271
272 if $GREP -q 'photon' /proc/version; then
273 echo 'photon'; return
274 fi
275
276 if $GREP -q 'ploplinux' /proc/version; then
277 echo 'ploplinux'; return
278 fi
279
280 if $GREP -q 'lunar' /proc/version; then
281 echo 'lunar'; return
282 fi
283
284 if $GREP -q 'SMGL-' /proc/version; then
285 echo 'smgl'; return
286 fi
287
288 if $GREP -q 'rancher' /proc/version; then
289 echo 'rancher'; return
290 fi
291
292
293 if [ -e /init ]; then
294 if $GREP -q -m1 'T2 SDE' /init; then
295 echo 't2'; return
296 fi
297 fi
298
299 if $GREP -q 'wifislax' /proc/version; then
300 echo 'wifislax'; return
301 fi
302
303 if $GREP -q 'pisilinux' /proc/version; then
304 echo 'pisilinux'; return
305 fi
306
307 if $GREP -q 'blackPanther' /proc/version; then
308 echo 'blackPanther'; return
309 fi
310
311 if $GREP -q 'primeos' /proc/version; then
312 echo 'primeos'; return
313 fi
314
315 if $GREP -q 'austrumi' /proc/version; then
316 echo 'austrumi'; return
317 fi
318
319 if [ -f /DISTRO_SPECS ]; then
320 if $GREP -q '[Pp]uppy' /DISTRO_SPECS; then
321 echo 'debian'; return
322 elif $GREP -q 'veket' /DISTRO_SPECS; then
323 echo 'debian'; return
324 fi
325 fi
326
327 if [ -f /etc/openEuler-release ]; then
328 echo "openEuler"; return
329 fi
330
331 echo "default"
332 }
333
334 VTOS=$(ventoy_get_os_type)
335 echo "OS=###${VTOS}###" >>$VTLOG
336 if [ -e "$VTOY_PATH/hook/$VTOS/ventoy-hook.sh" ]; then
337 $BUSYBOX_PATH/sh "$VTOY_PATH/hook/$VTOS/ventoy-hook.sh"
338 fi
339
340
341 ####################################################################
342 # #
343 # Step 3 : Check for debug break #
344 # #
345 ####################################################################
346 if [ "$VTOY_BREAK_LEVEL" = "03" ] || [ "$VTOY_BREAK_LEVEL" = "13" ]; then
347 $SLEEP 5
348 echo -e "\n\n\033[32m ################################################# \033[0m"
349 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
350 echo -e "\033[32m ################################################# \033[0m \n"
351 if [ "$VTOY_BREAK_LEVEL" = "13" ]; then
352 $CAT $VTOY_PATH/log
353 fi
354 exec $BUSYBOX_PATH/sh
355 fi
356
357
358 ####################################################################
359 # #
360 # Step 4 : Hand over to real init #
361 # #
362 ####################################################################
363 $BUSYBOX_PATH/umount /proc
364 if [ "$rmproc" = "Y" ]; then
365 $BUSYBOX_PATH/rm -rf /proc
366 fi
367
368 if [ -f $VTOY_PATH/ventoy_persistent_map ]; then
369 export PERSISTENT='YES'
370 export PERSISTENCE='true'
371 fi
372
373 cd /
374
375 unset VTLOG FIND GREP EGREP CAT AWK SED SLEEP HEAD vtcmdline
376
377 for vtinit in $user_rdinit /init /sbin/init /linuxrc; do
378 if [ -d /ventoy_rdroot ]; then
379 if [ -e "/ventoy_rdroot$vtinit" ]; then
380 # switch_root will check /init file, this is a cheat code
381 echo 'switch_root' > /init
382 exec $BUSYBOX_PATH/switch_root /ventoy_rdroot "$vtinit"
383 fi
384 else
385 if [ -e "$vtinit" ];then
386 if [ -f "$VTOY_PATH/hook/$VTOS/ventoy-before-init.sh" ]; then
387 $BUSYBOX_PATH/sh "$VTOY_PATH/hook/$VTOS/ventoy-before-init.sh"
388 fi
389 exec "$vtinit"
390 fi
391 fi
392 done
393
394 # Should never reach here
395 echo -e "\n\n\033[31m ############ INIT NOT FOUND ############### \033[0m \n"
396 exec $BUSYBOX_PATH/sh