]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - IMG/cpio/ventoy/init
1.0.05 release
[Ventoy.git] / IMG / cpio / ventoy / init
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 # #
23 # Step 1 : parse kernel debug parameter #
24 # #
25 ####################################################################
26 [ -d /proc ] || mkdir /proc; mount -t proc proc /proc
27 vtcmdline=$(cat /proc/cmdline)
28 vtkerver=$(cat /proc/version)
29 umount /proc; rm -rf /proc
30
31 echo "kenel version=$vtkerver" >>$VTLOG
32 echo "kenel cmdline=$vtcmdline" >>$VTLOG
33
34 #break here for debug
35 if [ "$VTOY_BREAK_LEVEL" = "01" ] || [ "$VTOY_BREAK_LEVEL" = "11" ]; then
36 sleep 5
37 echo -e "\n\n\033[32m ################################################# \033[0m"
38 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
39 echo -e "\033[32m ################################################# \033[0m \n"
40
41 if [ "$VTOY_BREAK_LEVEL" = "11" ]; then
42 cat $VTLOG
43 fi
44 exec $BUSYBOX_PATH/sh
45 fi
46
47
48 ####################################################################
49 # #
50 # Step 2 : extract real initramfs to / #
51 # #
52 ####################################################################
53 cd /
54 rm -rf /init /linuxrc /sbin /dev/ /root
55
56 ventoy_is_initrd_ramdisk() {
57 #As I known, PCLinuxOS use ramdisk
58 if echo $vtkerver | grep -i -q 'PCLinuxOS'; then
59 true
60 else
61 false
62 fi
63 }
64
65 # param: file skip magic tmp
66 ventoy_unpack_initramfs() {
67 vtfile=$1; vtskip=$2; vtmagic=$3; vttmp=$4
68 echo "=====ventoy_unpack_initramfs: #$*#" >> $VTLOG
69
70 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
71 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
72 echo "vtx=$vtx" >> $VTLOG
73 if [ $vtskip -eq 0 ]; then
74 ${vtx:5} $vtfile | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
75 else
76 dd if=$vtfile skip=$vtskip iflag=skip_bytes status=none | ${vtx:5} | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
77 fi
78 break
79 fi
80 done
81 }
82
83 # param: file magic tmp
84 ventoy_unpack_initrd() {
85 vtfile=$1; vtmagic=$2; vttmp=$3
86 echo "=====ventoy_unpack_initrd: #$*#" >> $VTLOG
87
88 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
89 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
90 echo "vtx=$vtx" >> $VTLOG
91 ${vtx:5} $vtfile > $vttmp
92 break
93 fi
94 done
95 }
96
97
98 # This export is for busybox cpio command
99 export EXTRACT_UNSAFE_SYMLINKS=1
100
101 # special process
102 need_xzminidec() {
103 testmagic=$(hexdump -n 2 -e '2/1 "%02X"' /initrd001)
104 if [ "FD37" = "${testmagic:0:4}" ]; then
105 if echo $vtkerver | grep -q 'kaspersky'; then
106 true
107 elif echo $vtkerver | grep -q 'kiosk.*Gentoo'; then
108 true
109 else
110 false
111 fi
112 else
113 false
114 fi
115 }
116
117 if need_xzminidec; then
118 echo "use xzminidec" >> $VTLOG
119 cat /initrd001 | xzminidec | cpio -idmu 2>>$VTLOG
120 rm -f /initrd001
121 else
122 for vtfile in $(ls /initrd*); do
123 #decompress first initrd
124 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $vtfile)
125
126 if ventoy_is_initrd_ramdisk; then
127 ventoy_unpack_initrd $vtfile $vtmagic ${vtfile}_tmp
128 mv ${vtfile}_tmp $vtfile
129 break
130 else
131 ventoy_unpack_initramfs $vtfile 0 $vtmagic ${vtfile}_tmp
132 fi
133
134 #only for cpio,cpio,...,initrd sequence, initrd,cpio or initrd,initrd sequence is not supported
135 while [ -e ${vtfile}_tmp ] && [ $(stat -c '%s' ${vtfile}_tmp) -gt 512 ]; do
136 mv ${vtfile}_tmp $vtfile
137 vtdump=$(hexdump -n 512 -e '512/1 "%02X"' $vtfile)
138 vtmagic=$(echo $vtdump | sed 's/^\(00\)*//')
139 let vtoffset="(${#vtdump}-${#vtmagic})/2"
140
141 if [ -z "$vtmagic" ]; then
142 echo "terminate with all zero data file" >> $VTLOG
143 break
144 fi
145
146 ventoy_unpack_initramfs $vtfile $vtoffset ${vtmagic:0:4} ${vtfile}_tmp
147 done
148
149 rm -f $vtfile ${vtfile}_tmp
150 done
151 fi
152
153 #break here for debug
154 if [ "$VTOY_BREAK_LEVEL" = "02" ] || [ "$VTOY_BREAK_LEVEL" = "12" ]; then
155 sleep 5
156 echo -e "\n\n\033[32m ################################################# \033[0m"
157 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
158 echo -e "\033[32m ################################################# \033[0m \n"
159 if [ "$VTOY_BREAK_LEVEL" = "12" ]; then
160 cat $VTOY_PATH/log
161 fi
162 exec $BUSYBOX_PATH/sh
163 fi
164
165
166 ####################################################################
167 # #
168 # Step 3 : Hand over to ventoy.sh #
169 # #
170 ####################################################################
171 echo "Now hand over to ventoy.sh" >>$VTLOG
172 . $VTOY_PATH/tool/vtoytool_install.sh
173
174 export PATH=$VTOY_ORG_PATH
175 exec $BUSYBOX_PATH/sh $VTOY_PATH/ventoy.sh