]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - IMG/cpio/ventoy/init_chain
support eweOS ISO (#3068)
[Ventoy.git] / IMG / cpio / ventoy / init_chain
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 : extract real initramfs to / #
24 # #
25 ####################################################################
26 cd /
27 rm -rf /init /linuxrc /dev/ /root
28
29 vtSbinFileNum=$(ls -1 /sbin | wc -l)
30 if [ $vtSbinFileNum -eq 1 ]; then
31 echo "remove whole sbin directory" >> $VTLOG
32 rm -rf /sbin
33 else
34 echo "remove only sbin/init file" >> $VTLOG
35 ls -l /sbin >> $VTLOG
36 rm -f /sbin/init
37 fi
38
39 ventoy_is_initrd_ramdisk() {
40 #As I known, PCLinuxOS/smgl use ramdisk
41 if echo $vtkerver | grep -i -q 'PCLinuxOS'; then
42 true
43 elif echo $vtkerver | grep -i -q 'SMGL-'; then
44 true
45 else
46 false
47 fi
48 }
49
50 ventoy_mount_squashfs() {
51 mkdir /dev
52 mount -t devtmpfs devtmpfs /dev
53 dd if=$1 of=/dev/ram0 status=none
54 umount /dev && rm -rf /dev
55 }
56
57 # param: file skip magic tmp
58 ventoy_unpack_initramfs() {
59 vtfile=$1; vtskip=$2; vtmagic=$3; vttmp=$4
60 echo "=====ventoy_unpack_initramfs: #$*#" >> $VTLOG
61
62 #special process
63 #if [ "${vtmagic:0:4}" = '5678' ]; then
64 # echo -en '\x1F\x8B' | dd status=none of=$vtfile bs=1 count=2 conv=notrunc
65 # vtmagic='1F8B'
66 #fi
67
68 if [ "${vtmagic:0:4}" = '6873' ]; then
69 ventoy_mount_squashfs $vtfile
70 return
71 fi
72
73 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat' '4C5A lunzip -c'; do
74 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
75 echo "vtx=$vtx" >> $VTLOG
76 if [ $vtskip -eq 0 ]; then
77 if [ "${vtx:5}" = "xzcat" ]; then
78 rm -f $VTOY_PATH/xzlog
79 ${vtx:5} $vtfile 2> $VTOY_PATH/xzlog | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
80 if grep -q 'corrupted data' $VTOY_PATH/xzlog; then
81 echo 'xzcat failed, now try xzminidec...' >> $VTLOG
82 rm -f $VTOY_PATH/xzlog
83 cat $vtfile | xzminidec 2> $VTOY_PATH/xzlog | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
84
85 if grep -q 'limit' $VTOY_PATH/xzlog; then
86 echo 'xzminidec failed, now try xzcat_musl ...' >> $VTLOG
87 xzcat_musl $vtfile | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
88 fi
89 fi
90 else
91 ${vtx:5} $vtfile | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
92 fi
93 else
94 dd if=$vtfile skip=$vtskip iflag=skip_bytes status=none | ${vtx:5} | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
95 fi
96 break
97 fi
98 done
99 }
100
101 # param: file magic tmp
102 ventoy_unpack_initrd() {
103 vtfile=$1; vtmagic=$2; vttmp=$3
104 echo "=====ventoy_unpack_initrd: #$*#" >> $VTLOG
105
106 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
107 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
108 echo "vtx=$vtx" >> $VTLOG
109 ${vtx:5} $vtfile > $vttmp
110 break
111 fi
112 done
113 }
114
115 vtfile_head_zero() {
116 local vsize
117 local voffset
118 local vfile
119 local vzero
120 local vdump
121
122 voffset=0
123 vfile=$1
124 vsize=$(stat -c '%s' ${vfile})
125 vzero=$(hexdump -n 512 -e '512/1 "%02X"' $vfile)
126
127 while [ $voffset -lt $vsize ]; do
128 vdump=$(hexdump -s $voffset -n 512 -e '512/1 "%02X"' $vfile)
129 if [ "$vdump" != "$vzero" ]; then
130 echo $voffset
131 return
132 fi
133 voffset=$($BUSYBOX_PATH/expr $voffset + 512)
134 done
135 echo 0
136 }
137
138 # This export is for busybox cpio command
139 export EXTRACT_UNSAFE_SYMLINKS=1
140
141 for vtfile in $(ls /initrd*); do
142 #decompress first initrd
143 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $vtfile)
144
145 if ventoy_is_initrd_ramdisk; then
146 ventoy_unpack_initrd $vtfile $vtmagic ${vtfile}_tmp
147 mv ${vtfile}_tmp $vtfile
148 break
149 else
150 ventoy_unpack_initramfs $vtfile 0 $vtmagic ${vtfile}_tmp
151 fi
152
153 #only for cpio,cpio,...,initrd sequence, initrd,cpio or initrd,initrd sequence is not supported
154 while [ -e ${vtfile}_tmp ] && [ $(stat -c '%s' ${vtfile}_tmp) -gt 512 ]; do
155 mv ${vtfile}_tmp $vtfile
156
157 vtdump=$(hexdump -n 512 -e '512/1 "%02X"' $vtfile)
158 vtmagic=$(echo $vtdump | sed 's/^\(00\)*//')
159 let vtoffset="(${#vtdump}-${#vtmagic})/2"
160
161 if [ -z "$vtmagic" ]; then
162 vtHeadZero=$(vtfile_head_zero $vtfile)
163 if [ $vtHeadZero -gt 0 ]; then
164 vtdump=$(hexdump -s $vtHeadZero -n 512 -e '512/1 "%02X"' $vtfile)
165 vtmagic=$(echo $vtdump | sed 's/^\(00\)*//')
166 let vtoffset="(${#vtdump}-${#vtmagic})/2+$vtHeadZero"
167 echo "skip head $vtHeadZero zeros with magic ${vtmagic:0:4}" >> $VTLOG
168 else
169 echo "terminate with all zero data file" >> $VTLOG
170 break
171 fi
172 fi
173
174 ventoy_unpack_initramfs $vtfile $vtoffset ${vtmagic:0:4} ${vtfile}_tmp
175 done
176
177 rm -f $vtfile ${vtfile}_tmp
178 done
179
180
181 #break here for debug
182 if [ "$VTOY_BREAK_LEVEL" = "02" ] || [ "$VTOY_BREAK_LEVEL" = "12" ]; then
183 sleep 5
184 echo -e "\n\n\033[32m ################################################# \033[0m"
185 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
186 echo -e "\033[32m ################################################# \033[0m \n"
187 if [ "$VTOY_BREAK_LEVEL" = "12" ]; then
188 cat $VTOY_PATH/log
189 fi
190 exec $BUSYBOX_PATH/sh
191 fi
192
193
194 ####################################################################
195 # #
196 # Step 3 : Extract injection archive #
197 # #
198 ####################################################################
199 ventoy_unpack_injection() {
200 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $VTOY_PATH/ventoy_injection)
201 echo "ventoy_unpack_injection vtmagic=$vtmagic ..."
202
203 if [ "1F8B" = "$vtmagic" ] || [ "1F9E" = "$vtmagic" ]; then
204 echo "tar.gz tar -xzvf"
205 tar -xzvf $VTOY_PATH/ventoy_injection -C /
206 elif [ "425A" = "$vtmagic" ]; then
207 echo "tar.bz2 tar -xjvf"
208 tar -xjvf $VTOY_PATH/ventoy_injection -C /
209 elif [ "FD37" = "$vtmagic" ]; then
210 echo "tar.xz tar -xJvf"
211 tar -xJvf $VTOY_PATH/ventoy_injection -C /
212 elif [ "5D00" = "$vtmagic" ]; then
213 echo "tar.lzma tar -xavf"
214 tar -xavf $VTOY_PATH/ventoy_injection -C /
215 else
216 echo "unzip -o"
217 unzip -o $VTOY_PATH/ventoy_injection -d /
218 fi
219 }
220
221 if [ -e $VTOY_PATH/ventoy_injection ]; then
222 echo "### decompress injection ... ###" >>$VTLOG
223 ventoy_unpack_injection > $VTOY_PATH/injection.log 2>&1
224 fi
225
226
227 ####################################################################
228 # #
229 # Step 4 : Hand over to ventoy_chain.sh #
230 # #
231 ####################################################################
232 echo "Now hand over to ventoy.sh" >>$VTLOG
233 . $VTOY_PATH/tool/vtoytool_install.sh
234
235 export PATH=$VTOY_ORG_PATH
236 exec $BUSYBOX_PATH/sh $VTOY_PATH/ventoy_chain.sh