ventoy_loop.sh 5.31 KB
Newer Older
longpanda's avatar
longpanda committed
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
#!/ventoy/busybox/sh
#************************************************************************************
# Copyright (c) 2020, longpanda <admin@ventoy.net>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
# 
#************************************************************************************

####################################################################
#                                                                  #
# Step 1 : Parse kernel parameter                                  #
#                                                                  #
####################################################################
if ! [ -e /proc ]; then
    $BUSYBOX_PATH/mkdir /proc
    rmproc='Y'
fi
$BUSYBOX_PATH/mount -t proc proc /proc

# vtinit=xxx to replace rdinit=xxx
vtcmdline=$($CAT /proc/cmdline)
for i in $vtcmdline; do
    if echo $i | $GREP -q vtinit; then
        user_rdinit=${i#vtinit=}
        echo "user set user_rdinit=${user_rdinit}" >>$VTLOG
    fi
done

longpanda's avatar
update  
longpanda committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
####################################################################
#                                                                  #
# Step 2 : Process ko                                              #
#                                                                  #
####################################################################
$BUSYBOX_PATH/mkdir -p /ventoy/modules
$BUSYBOX_PATH/ls -1a / | $EGREP '\.ko$|\.ko.[gx]z$' | while read vtline; do
    if [ "${vtline:0:1}" = "." ]; then
        $BUSYBOX_PATH/mv /${vtline} /ventoy/modules/${vtline:1}
    else
        $BUSYBOX_PATH/mv /${vtline} /ventoy/modules/
    fi
done

if [ -e /vtloopex.tar.xz ]; then
    echo "extract vtloopex ..." >> $VTLOG
    $BUSYBOX_PATH/tar -xJf /vtloopex.tar.xz -C $VTOY_PATH/
    $BUSYBOX_PATH/rm -f /vtloopex.tar.xz
fi

longpanda's avatar
longpanda committed
60
61
62

####################################################################
#                                                                  #
longpanda's avatar
update  
longpanda committed
63
# Step 3 : Do OS specific hook                                     #
longpanda's avatar
longpanda committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#                                                                  #
####################################################################
ventoy_get_os_type() {
    echo "kernel version" >> $VTLOG
    $CAT /proc/version >> $VTLOG

    # deepin-live
    if $GREP -q 'deepin' /proc/version; then
        echo 'deepin'; return
    fi
    
    if $GREP -q 'endless' /proc/version; then
        echo 'endless'; return
    fi
    
longpanda's avatar
update  
longpanda committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    if $GREP -q 'OpenWrt' /proc/version; then
        echo 'openwrt'; return
    fi
    
    if [ -e /BOOT_SPECS ]; then
        if $GREP -q 'easyos' /BOOT_SPECS; then
            echo 'easyos'; return
        fi
    fi
    
    if [ -e /etc/os-release ]; then
        if $GREP -q 'volumio' /etc/os-release; then
            echo 'volumio'; return
        fi
    fi
    
longpanda's avatar
longpanda committed
95
96
97
98
99
100
101
102
103
104
105
106
    echo "default"
}

VTOS=$(ventoy_get_os_type)
echo "OS=###${VTOS}###" >>$VTLOG
if [ -e "$VTOY_PATH/loop/$VTOS/ventoy-hook.sh" ]; then
    $BUSYBOX_PATH/sh "$VTOY_PATH/loop/$VTOS/ventoy-hook.sh"
fi


####################################################################
#                                                                  #
longpanda's avatar
update  
longpanda committed
107
# Step 4 : Check for debug break                                   #
longpanda's avatar
longpanda committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#                                                                  #
####################################################################
if [ "$VTOY_BREAK_LEVEL" = "03" ] || [ "$VTOY_BREAK_LEVEL" = "13" ]; then
    $SLEEP 5
    echo -e "\n\n\033[32m ################################################# \033[0m"
    echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
    echo -e "\033[32m ################################################# \033[0m \n"
    if [ "$VTOY_BREAK_LEVEL" = "13" ]; then 
        $CAT $VTOY_PATH/log
    fi
    exec $BUSYBOX_PATH/sh
fi


####################################################################
#                                                                  #
longpanda's avatar
update  
longpanda committed
124
# Step 5 : Hand over to real init                                  #
longpanda's avatar
longpanda committed
125
126
127
128
129
130
131
132
133
134
135
#                                                                  #
####################################################################
$BUSYBOX_PATH/umount /proc
if [ "$rmproc" = "Y" ]; then
    $BUSYBOX_PATH/rm -rf /proc
fi

cd /

unset VTLOG FIND GREP EGREP CAT AWK SED SLEEP HEAD

longpanda's avatar
update  
longpanda committed
136
for vtinit in $user_rdinit /init /sbin/init  /linuxrc; do
longpanda's avatar
longpanda committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    if [ -d /ventoy_rdroot ]; then
        if [ -e "/ventoy_rdroot$vtinit" ]; then
            # switch_root will check /init file, this is a cheat code
            echo 'switch_root' > /init
            exec $BUSYBOX_PATH/switch_root /ventoy_rdroot "$vtinit"
        fi
    else
        if [ -e "$vtinit" ];then
            exec "$vtinit"
        fi
    fi
done

# Should never reach here
echo -e "\n\n\033[31m ############ INIT NOT FOUND ############### \033[0m \n"
exec $BUSYBOX_PATH/sh