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