ventoy_chain.sh 13.2 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
40
41
42
43
44
45
46
47
48
#!/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

####################################################################
#                                                                  #
# Step 2 : Do OS specific hook                                     #
#                                                                  #
####################################################################
ventoy_get_os_type() {
    echo "kernel version" >> $VTLOG
    $CAT /proc/version >> $VTLOG

longpanda's avatar
longpanda committed
49
50
51
52
53
54
    if [ -d /twres ]; then
        if $GREP -q 'Phoenix' /init; then
            echo 'phoenixos'; return
        fi
    fi

55
56
57
58
    # PrimeOS :
    if $GREP -q 'PrimeOS' /proc/version; then
        echo 'primeos'; return

59
    # Debian :
60
    elif $GREP -q '[Dd]ebian' /proc/version; then
61
62
63
64
65
66
67
68
69
70
        echo 'debian'; return

    # Ubuntu : do the same process with debian
    elif $GREP -q '[Uu]buntu' /proc/version; then
        echo 'debian'; return
        
    # Deepin : do the same process with debian
    elif $GREP -q '[Dd]eepin' /proc/version; then
        echo 'debian'; return

longpanda's avatar
longpanda committed
71
    # rhel5/CentOS5 and all other distributions based on them
72
    elif $GREP -q 'el5' /proc/version; then
longpanda's avatar
longpanda committed
73
74
75
76
        echo 'rhel5'; return

    # rhel6/CentOS6 and all other distributions based on them
    elif $GREP -q 'el6' /proc/version; then
longpanda's avatar
longpanda committed
77
78
79
80
81
82
        if [ -f /sbin/detectcd ]; then
            if $GREP -q -i 'LENOVO-EasyStartup' /sbin/detectcd; then
                echo 'easystartup'; return
            fi
        fi

longpanda's avatar
longpanda committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        echo 'rhel6'; return

    # rhel7/CentOS7/rhel8/CentOS8 and all other distributions based on them
    elif $GREP -q 'el[78]' /proc/version; then
        echo 'rhel7'; return   

    # Maybe rhel9 rhel1x use the same way? Who knows!
    elif $EGREP -q 'el9|el1[0-9]' /proc/version; then
        echo 'rhel7'; return   
        
    # Fedora : do the same process with rhel7
    elif $GREP -q '\.fc[0-9][0-9]\.' /proc/version; then
        echo 'rhel7'; return
        
    # SUSE
    elif $GREP -q 'SUSE' /proc/version; then
        echo 'suse'; return
        
    # ArchLinux
    elif $EGREP -q 'archlinux|ARCH' /proc/version; then
        echo 'arch'; return
    
longpanda's avatar
longpanda committed
105
106
107
108
    # kiosk
    elif $EGREP -q 'kiosk' /proc/version; then
        echo 'kiosk'; return
    
longpanda's avatar
longpanda committed
109
110
    # gentoo
    elif $EGREP -q '[Gg]entoo' /proc/version; then
longpanda's avatar
longpanda committed
111
112
113
114
        if $GREP -q 'daphile' /proc/version; then
            echo 'daphile'; return
        fi
    
longpanda's avatar
longpanda committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        echo 'gentoo'; return
        
    # TinyCore
    elif $EGREP -q 'tinycore' /proc/version; then
        echo 'tinycore'; return
    
    # manjaro
    elif $EGREP -q 'manjaro|MANJARO' /proc/version; then
        echo 'manjaro'; return
        
    # mageia
    elif $EGREP -q 'mageia' /proc/version; then
        echo 'mageia'; return
    
    # pclinux OS
    elif $GREP -i -q 'PCLinuxOS' /proc/version; then
        echo 'pclos'; return
    
    # KaOS
    elif $GREP -i -q 'kaos' /proc/version; then
        echo 'kaos'; return
    
    # Alpine
    elif $GREP -q 'Alpine' /proc/version; then
        echo 'alpine'; return

longpanda's avatar
longpanda committed
141
142
143
    elif $GREP -i -q 'PhoenixOS' /proc/version; then
        echo 'phoenixos'; return
    
longpanda's avatar
longpanda committed
144
145
146
147
    # NixOS
    elif $GREP -i -q 'NixOS' /proc/version; then
        echo 'nixos'; return
    
longpanda's avatar
longpanda committed
148
    
longpanda's avatar
longpanda committed
149
150
151
152
153
154
155
156
157
158
    fi

    if [ -e /lib/debian-installer ]; then
        echo 'debian'; return
    fi

    if [ -e /etc/os-release ]; then
        if $GREP -q 'XenServer' /etc/os-release; then
            echo 'xen'; return
        elif $GREP -q 'SUSE ' /etc/os-release; then
longpanda's avatar
longpanda committed
159
160
161
            echo 'suse'; return        
        elif $GREP -q 'uruk' /etc/os-release; then
            echo 'debian'; return
longpanda's avatar
longpanda committed
162
163
        elif $GREP -q 'Solus' /etc/os-release; then
            echo 'rhel7'; return
longpanda's avatar
longpanda committed
164
165
        elif $GREP -q 'openEuler' /etc/os-release; then
            echo 'openEuler'; return
longpanda's avatar
longpanda committed
166
        elif $GREP -q 'fuyu' /etc/os-release; then
167
168
169
170
171
            echo 'openEuler'; return
        elif $GREP -q 'deepin' /etc/os-release; then
            echo 'debian'; return
        elif $GREP -q 'chinauos' /etc/os-release; then
            echo 'debian'; return
longpanda's avatar
longpanda committed
172
173
174
175
176
177
178
179
180
181
182
183
        fi
    fi
    
    if $BUSYBOX_PATH/dmesg | $GREP -q -m1 "Xen:"; then
        echo 'xen'; return
    fi
    
    
    if [ -e /etc/HOSTNAME ] && $GREP -i -q 'slackware' /etc/HOSTNAME; then
        echo 'slackware'; return
    fi
    
longpanda's avatar
longpanda committed
184
185
186
187
188
189
    if [ -e /init ]; then
        if $GREP -i -q zeroshell /init; then
            echo 'zeroshell'; return
        fi
    fi
    
longpanda's avatar
longpanda committed
190
191
192
193
194
195
196
    if $EGREP -q 'ALT ' /proc/version; then
        echo 'alt'; return
    fi
    
    if $EGREP -q 'porteus' /proc/version; then
        echo 'debian'; return
    fi
longpanda's avatar
longpanda committed
197
    
longpanda's avatar
longpanda committed
198
199
200
201
202
203
204
205
    if $GREP -q 'Clear Linux ' /proc/version; then
        echo 'clear'; return
    fi
    
    if $GREP -q 'artix' /proc/version; then
        echo 'arch'; return
    fi
    
longpanda's avatar
longpanda committed
206
207
208
209
    if $GREP -q 'berry ' /proc/version; then
        echo 'berry'; return
    fi
    
longpanda's avatar
longpanda committed
210
211
212
213
214
215
216
    if $GREP -q 'Gobo ' /proc/version; then
        echo 'gobo'; return
    fi
    
    if $GREP -q 'NuTyX' /proc/version; then
        echo 'nutyx'; return
    fi
longpanda's avatar
longpanda committed
217
    
longpanda's avatar
longpanda committed
218
219
220
221
222
223
    if [ -d /gnu ]; then
        vtLineNum=$($FIND /gnu/ -name guix | $BUSYBOX_PATH/wc -l)
        if [ $vtLineNum -gt 0 ]; then
            echo 'guix'; return
        fi
    fi
longpanda's avatar
longpanda committed
224
    
longpanda's avatar
longpanda committed
225
226
227
228
    if $GREP -q 'android.x86' /proc/version; then
        echo 'android'; return
    fi 
    
longpanda's avatar
longpanda committed
229
230
231
232
    if $GREP -q 'adelielinux' /proc/version; then
        echo 'adelie'; return
    fi
    
longpanda's avatar
longpanda committed
233
234
235
236
    if $GREP -q 'pmagic' /proc/version; then
        echo 'pmagic'; return
    fi
    
longpanda's avatar
longpanda committed
237
238
239
240
    if $GREP -q 'CDlinux' /proc/cmdline; then
        echo 'cdlinux'; return
    fi
    
longpanda's avatar
longpanda committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    if $GREP -q 'parabola' /proc/version; then
        echo 'parabola'; return
    fi
    
    if $GREP -q 'cucumber' /proc/version; then
        echo 'cucumber'; return
    fi
    
    if $GREP -q 'fatdog' /proc/version; then
        echo 'fatdog'; return
    fi
    
    if $GREP -q 'KWORT' /proc/version; then
        echo 'kwort'; return
    fi
    
longpanda's avatar
longpanda committed
257
258
259
    if $GREP -q 'iwamoto' /proc/version; then
        echo 'vine'; return
    fi
longpanda's avatar
longpanda committed
260
    
longpanda's avatar
update  
longpanda committed
261
262
263
264
    if $GREP -q 'hyperbola' /proc/cmdline; then
        echo 'hyperbola'; return
    fi
    
longpanda's avatar
longpanda committed
265
266
267
268
    if $GREP -q 'CRUX' /proc/version; then
        echo 'crux'; return
    fi
    
longpanda's avatar
longpanda committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    if [ -f /init ]; then
        if $GREP -q 'AryaLinux' /init; then
            echo 'aryalinux'; return
        elif $GREP -q 'Dragora' /init; then
            echo 'dragora'; return
            
        fi
    fi
    
    if $GREP -q 'slackware' /proc/version; then
        echo 'slackware'; return
    fi
    
    if $BUSYBOX_PATH/hostname | $GREP -q 'smoothwall'; then
        echo 'smoothwall'; return
    fi 
    
    if $GREP -q 'photon' /proc/version; then
        echo 'photon'; return
    fi
    
    if $GREP -q 'ploplinux' /proc/version; then
        echo 'ploplinux'; return
    fi
    
    if $GREP -q 'lunar' /proc/version; then
        echo 'lunar'; return
    fi
    
    if $GREP -q 'SMGL-' /proc/version; then
        echo 'smgl'; return
    fi
    
    if $GREP -q 'rancher' /proc/version; then
        echo 'rancher'; return
    fi
    
    
    if [ -e /init ]; then
        if $GREP -q -m1 'T2 SDE' /init; then
            echo 't2'; return
        fi
    fi
    
    if $GREP -q 'wifislax' /proc/version; then
        echo 'wifislax'; return
    fi
    
    if $GREP -q 'pisilinux' /proc/version; then
        echo 'pisilinux'; return
    fi
    
longpanda's avatar
longpanda committed
321
322
323
324
    if $GREP -q 'blackPanther' /proc/version; then
        echo 'blackPanther'; return
    fi
    
longpanda's avatar
longpanda committed
325
326
327
328
    if $GREP -q 'primeos' /proc/version; then
        echo 'primeos'; return
    fi
    
longpanda's avatar
longpanda committed
329
330
331
332
    if $GREP -q 'austrumi' /proc/version; then
        echo 'austrumi'; return
    fi
    
333
334
335
    if [ -f /DISTRO_SPECS ]; then
        if $GREP -q '[Pp]uppy' /DISTRO_SPECS; then
            echo 'debian'; return
longpanda's avatar
longpanda committed
336
337
        elif $GREP -q 'veket' /DISTRO_SPECS; then
            echo 'debian'; return
338
339
340
        fi
    fi
    
longpanda's avatar
longpanda committed
341
342
343
344
    if [ -f /etc/openEuler-release ]; then
        echo "openEuler"; return
    fi
    
longpanda's avatar
longpanda committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    
    #special arch based iso file check
    if [ -f /init ]; then
        if $GREP -q 'mount_handler' /init; then
            if [ -d /hooks ]; then
                if $BUSYBOX_PATH/ls -1 /hooks/ | $GREP -q '.*iso$'; then
                    echo "arch"; return
                fi
            elif [ -d /hook ]; then
                if $BUSYBOX_PATH/ls -1 /hook/ | $GREP -q '.*iso$'; then
                    echo "arch"; return
                fi
            fi
        fi
longpanda's avatar
longpanda committed
359
360
    fi
    
longpanda's avatar
longpanda committed
361
362
    
    
longpanda's avatar
longpanda committed
363
364
365
366
367
368
369
370
371
372
    echo "default"
}

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


longpanda's avatar
longpanda committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

if $GREP -q -i Untangle /proc/version; then
    for vtPara in $($CAT /proc/cmdline); do
        vtItemkey=$(echo $vtPara | $AWK -F= '{print $1}')
        vtItemVal=$(echo $vtPara | $AWK -F= '{print $2}')
        if $GREP -q -m1 "^$vtItemkey\$" $VTOY_PATH/hook/default/export.list; then
            vtEnvExport="$vtEnvExport $vtItemkey=$vtItemVal"
        fi
    done
    
    echo "================ env export ================" >> $VTLOG
    echo $vtEnvExport >> $VTLOG
    echo "============================================" >> $VTLOG
fi



longpanda's avatar
longpanda committed
390
391
####################################################################
#                                                                  #
longpanda's avatar
longpanda committed
392
393
394
395
396
397
398
399
400
401
402
# Step 3 : Run LiveInjection Hook                                  #
#                                                                  #
####################################################################
if [ -f "/live_injection_7ed136ec_7a61_4b54_adc3_ae494d5106ea/hook.sh" ]; then
    $BUSYBOX_PATH/sh "/live_injection_7ed136ec_7a61_4b54_adc3_ae494d5106ea/hook.sh" $VTOS
fi


####################################################################
#                                                                  #
# Step 4 : Check for debug break                                   #
longpanda's avatar
longpanda committed
403
404
405
406
407
408
409
410
411
412
413
414
415
#                                                                  #
####################################################################
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
longpanda committed
416

longpanda's avatar
longpanda committed
417
418
####################################################################
#                                                                  #
longpanda's avatar
longpanda committed
419
# Step 5 : Hand over to real init                                  #
longpanda's avatar
longpanda committed
420
421
422
423
424
425
426
#                                                                  #
####################################################################
$BUSYBOX_PATH/umount /proc
if [ "$rmproc" = "Y" ]; then
    $BUSYBOX_PATH/rm -rf /proc
fi

longpanda's avatar
longpanda committed
427
428
429
430
431
if [ -f $VTOY_PATH/ventoy_persistent_map ]; then
    export PERSISTENT='YES'
    export PERSISTENCE='true'
fi

longpanda's avatar
longpanda committed
432
cd /
longpanda's avatar
longpanda committed
433

longpanda's avatar
longpanda committed
434
unset VTLOG FIND GREP EGREP CAT AWK SED SLEEP HEAD vtcmdline
longpanda's avatar
longpanda committed
435
436
437
438
439
440
441
442
443
444

for vtinit in $user_rdinit /init /sbin/init /linuxrc; do
    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
longpanda's avatar
longpanda committed
445
446
447
            if [ -f "$VTOY_PATH/hook/$VTOS/ventoy-before-init.sh" ]; then
                $BUSYBOX_PATH/sh "$VTOY_PATH/hook/$VTOS/ventoy-before-init.sh"
            fi
longpanda's avatar
longpanda committed
448
449
450
451
452
453
            
            if [ -z "$vtEnvExport" ]; then
                exec "$vtinit"
            else
                exec env $vtEnvExport "$vtinit"
            fi            
longpanda's avatar
longpanda committed
454
455
456
457
458
459
460
        fi
    fi
done

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