]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - IMG/cpio/ventoy/ventoy_loop.sh
update
[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 # deepin-live
71 if $GREP -q 'deepin' /proc/version; then
72 echo 'deepin'; return
73 fi
74
75 if $GREP -q 'endless' /proc/version; then
76 echo 'endless'; return
77 fi
78
79 if $GREP -q 'OpenWrt' /proc/version; then
80 echo 'openwrt'; return
81 fi
82
83 if [ -e /BOOT_SPECS ]; then
84 if $GREP -q 'easyos' /BOOT_SPECS; then
85 echo 'easyos'; return
86 fi
87 fi
88
89 if [ -e /etc/os-release ]; then
90 if $GREP -q 'volumio' /etc/os-release; then
91 echo 'volumio'; return
92 fi
93 fi
94
95 echo "default"
96 }
97
98 VTOS=$(ventoy_get_os_type)
99 echo "OS=###${VTOS}###" >>$VTLOG
100 if [ -e "$VTOY_PATH/loop/$VTOS/ventoy-hook.sh" ]; then
101 $BUSYBOX_PATH/sh "$VTOY_PATH/loop/$VTOS/ventoy-hook.sh"
102 fi
103
104
105 ####################################################################
106 # #
107 # Step 4 : Check for debug break #
108 # #
109 ####################################################################
110 if [ "$VTOY_BREAK_LEVEL" = "03" ] || [ "$VTOY_BREAK_LEVEL" = "13" ]; then
111 $SLEEP 5
112 echo -e "\n\n\033[32m ################################################# \033[0m"
113 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
114 echo -e "\033[32m ################################################# \033[0m \n"
115 if [ "$VTOY_BREAK_LEVEL" = "13" ]; then
116 $CAT $VTOY_PATH/log
117 fi
118 exec $BUSYBOX_PATH/sh
119 fi
120
121
122 ####################################################################
123 # #
124 # Step 5 : Hand over to real init #
125 # #
126 ####################################################################
127 $BUSYBOX_PATH/umount /proc
128 if [ "$rmproc" = "Y" ]; then
129 $BUSYBOX_PATH/rm -rf /proc
130 fi
131
132 cd /
133
134 unset VTLOG FIND GREP EGREP CAT AWK SED SLEEP HEAD
135
136 for vtinit in $user_rdinit /init /sbin/init /linuxrc; do
137 if [ -d /ventoy_rdroot ]; then
138 if [ -e "/ventoy_rdroot$vtinit" ]; then
139 # switch_root will check /init file, this is a cheat code
140 echo 'switch_root' > /init
141 exec $BUSYBOX_PATH/switch_root /ventoy_rdroot "$vtinit"
142 fi
143 else
144 if [ -e "$vtinit" ];then
145 exec "$vtinit"
146 fi
147 fi
148 done
149
150 # Should never reach here
151 echo -e "\n\n\033[31m ############ INIT NOT FOUND ############### \033[0m \n"
152 exec $BUSYBOX_PATH/sh