2 #************************************************************************************
3 # Copyright (c) 2020, longpanda <admin@ventoy.net>
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.
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.
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/>.
18 #************************************************************************************
21 ####################################################################
23 # Step 1 : extract real initramfs to / #
25 ####################################################################
27 rm -rf /init /linuxrc /dev/ /root
29 vtSbinFileNum=$(ls -1 /sbin | wc -l)
30 if [ $vtSbinFileNum -eq 1 ]; then
31 echo "remove whole sbin directory" >> $VTLOG
34 echo "remove only sbin/init file" >> $VTLOG
39 ventoy_is_initrd_ramdisk() {
40 #As I known, PCLinuxOS/smgl use ramdisk
41 if echo $vtkerver | grep -i -q 'PCLinuxOS'; then
43 elif echo $vtkerver | grep -i -q 'SMGL-'; then
50 ventoy_mount_squashfs() {
52 mount -t devtmpfs devtmpfs /dev
53 dd if=$1 of=/dev/ram0 status=none
54 umount /dev && rm -rf /dev
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
63 #if [ "${vtmagic:0:4}" = '5678' ]; then
64 # echo -en '\x1F\x8B' | dd status=none of=$vtfile bs=1 count=2 conv=notrunc
68 if [ "${vtmagic:0:4}" = '6873' ]; then
69 ventoy_mount_squashfs $vtfile
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 -ne 0 ]; then
77 dd if=$vtfile skip=$vtskip iflag=skip_bytes status=none > ${vtfile}.skip
79 mv ${vtfile}.skip $vtfile
82 if [ "${vtx:5}" = "xzcat" ]; then
83 rm -f $VTOY_PATH/xzlog
84 ${vtx:5} $vtfile 2> $VTOY_PATH/xzlog | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
85 if grep -q 'corrupted data' $VTOY_PATH/xzlog; then
86 echo 'xzcat failed, now try xzminidec...' >> $VTLOG
87 rm -f $VTOY_PATH/xzlog
88 cat $vtfile | xzminidec 2> $VTOY_PATH/xzlog | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
90 if grep -q 'limit' $VTOY_PATH/xzlog; then
91 echo 'xzminidec failed, now try xzcat_musl ...' >> $VTLOG
92 xzcat_musl $vtfile | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
96 ${vtx:5} $vtfile | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
103 # param: file magic tmp
104 ventoy_unpack_initrd() {
105 vtfile=$1; vtmagic=$2; vttmp=$3
106 echo "=====ventoy_unpack_initrd: #$*#" >> $VTLOG
108 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
109 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
110 echo "vtx=$vtx" >> $VTLOG
111 ${vtx:5} $vtfile > $vttmp
126 vsize=$(stat -c '%s' ${vfile})
127 vzero=$(hexdump -n 512 -e '512/1 "%02X"' $vfile)
129 while [ $voffset -lt $vsize ]; do
130 vdump=$(hexdump -s $voffset -n 512 -e '512/1 "%02X"' $vfile)
131 if [ "$vdump" != "$vzero" ]; then
135 voffset=$($BUSYBOX_PATH/expr $voffset + 512)
140 # This export is for busybox cpio command
141 export EXTRACT_UNSAFE_SYMLINKS=1
143 for vtfile in $(ls /initrd*); do
144 #decompress first initrd
145 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $vtfile)
147 if ventoy_is_initrd_ramdisk; then
148 ventoy_unpack_initrd $vtfile $vtmagic ${vtfile}_tmp
149 mv ${vtfile}_tmp $vtfile
152 ventoy_unpack_initramfs $vtfile 0 $vtmagic ${vtfile}_tmp
155 #only for cpio,cpio,...,initrd sequence, initrd,cpio or initrd,initrd sequence is not supported
156 while [ -e ${vtfile}_tmp ] && [ $(stat -c '%s' ${vtfile}_tmp) -gt 512 ]; do
157 mv ${vtfile}_tmp $vtfile
159 vtdump=$(hexdump -n 512 -e '512/1 "%02X"' $vtfile)
160 vtmagic=$(echo $vtdump | sed 's/^\(00\)*//')
161 let vtoffset="(${#vtdump}-${#vtmagic})/2"
163 if [ -z "$vtmagic" ]; then
164 vtHeadZero=$(vtfile_head_zero $vtfile)
165 if [ $vtHeadZero -gt 0 ]; then
166 vtdump=$(hexdump -s $vtHeadZero -n 512 -e '512/1 "%02X"' $vtfile)
167 vtmagic=$(echo $vtdump | sed 's/^\(00\)*//')
168 let vtoffset="(${#vtdump}-${#vtmagic})/2+$vtHeadZero"
169 echo "skip head $vtHeadZero zeros with magic ${vtmagic:0:4}" >> $VTLOG
171 echo "terminate with all zero data file" >> $VTLOG
176 ventoy_unpack_initramfs $vtfile $vtoffset ${vtmagic:0:4} ${vtfile}_tmp
179 rm -f $vtfile ${vtfile}_tmp
183 #break here for debug
184 if [ "$VTOY_BREAK_LEVEL" = "02" ] || [ "$VTOY_BREAK_LEVEL" = "12" ]; then
186 echo -e "\n\n\033[32m ################################################# \033[0m"
187 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
188 echo -e "\033[32m ################################################# \033[0m \n"
189 if [ "$VTOY_BREAK_LEVEL" = "12" ]; then
192 exec $BUSYBOX_PATH/sh
196 ####################################################################
198 # Step 3 : Extract injection archive #
200 ####################################################################
201 ventoy_unpack_injection() {
202 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $VTOY_PATH/ventoy_injection)
203 echo "ventoy_unpack_injection vtmagic=$vtmagic ..."
205 if [ "1F8B" = "$vtmagic" ] || [ "1F9E" = "$vtmagic" ]; then
206 echo "tar.gz tar -xzvf"
207 tar -xzvf $VTOY_PATH/ventoy_injection -C /
208 elif [ "425A" = "$vtmagic" ]; then
209 echo "tar.bz2 tar -xjvf"
210 tar -xjvf $VTOY_PATH/ventoy_injection -C /
211 elif [ "FD37" = "$vtmagic" ]; then
212 echo "tar.xz tar -xJvf"
213 tar -xJvf $VTOY_PATH/ventoy_injection -C /
214 elif [ "5D00" = "$vtmagic" ]; then
215 echo "tar.lzma tar -xavf"
216 tar -xavf $VTOY_PATH/ventoy_injection -C /
219 unzip -o $VTOY_PATH/ventoy_injection -d /
223 if [ -e $VTOY_PATH/ventoy_injection ]; then
224 echo "### decompress injection ... ###" >>$VTLOG
225 ventoy_unpack_injection > $VTOY_PATH/injection.log 2>&1
229 ####################################################################
231 # Step 4 : Hand over to ventoy_chain.sh #
233 ####################################################################
234 echo "Now hand over to ventoy.sh" >>$VTLOG
235 . $VTOY_PATH/tool/vtoytool_install.sh
237 export PATH=$VTOY_ORG_PATH
238 exec $BUSYBOX_PATH/sh $VTOY_PATH/ventoy_chain.sh