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 : parse kernel debug parameter #
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
31 echo "kenel version=$vtkerver" >>$VTLOG
32 echo "kenel cmdline=$vtcmdline" >>$VTLOG
35 if [ "$VTOY_BREAK_LEVEL" = "01" ] || [ "$VTOY_BREAK_LEVEL" = "11" ]; then
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"
41 if [ "$VTOY_BREAK_LEVEL" = "11" ]; then
48 ####################################################################
50 # Step 2 : extract real initramfs to / #
52 ####################################################################
54 rm -rf /init /linuxrc /dev/ /root
56 vtSbinFileNum=$(ls -1 /sbin | wc -l)
57 if [ $vtSbinFileNum -eq 1 ]; then
58 echo "remove whole sbin directory" >> $VTLOG
61 echo "remove only sbin/init file" >> $VTLOG
66 ventoy_is_initrd_ramdisk() {
67 #As I known, PCLinuxOS use ramdisk
68 if echo $vtkerver | grep -i -q 'PCLinuxOS'; then
75 ventoy_mount_squashfs() {
77 mount -t devtmpfs devtmpfs /dev
78 dd if=$1 of=/dev/ram0 status=none
79 umount /dev && rm -rf /dev
82 # param: file skip magic tmp
83 ventoy_unpack_initramfs() {
84 vtfile=$1; vtskip=$2; vtmagic=$3; vttmp=$4
85 echo "=====ventoy_unpack_initramfs: #$*#" >> $VTLOG
88 #if [ "${vtmagic:0:4}" = '5678' ]; then
89 # echo -en '\x1F\x8B' | dd status=none of=$vtfile bs=1 count=2 conv=notrunc
93 if [ "${vtmagic:0:4}" = '6873' ]; then
94 ventoy_mount_squashfs $vtfile
98 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
99 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
100 echo "vtx=$vtx" >> $VTLOG
101 if [ $vtskip -eq 0 ]; then
102 if [ "${vtx:5}" = "xzcat" ]; then
103 rm -f $VTOY_PATH/xzlog
104 ${vtx:5} $vtfile 2> $VTOY_PATH/xzlog | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
106 if grep -q 'corrupted data' $VTOY_PATH/xzlog; then
107 echo 'xzcat failed, now try xzminidec...' >> $VTLOG
108 cat $vtfile | xzminidec | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
112 ${vtx:5} $vtfile | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
115 dd if=$vtfile skip=$vtskip iflag=skip_bytes status=none | ${vtx:5} | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
122 # param: file magic tmp
123 ventoy_unpack_initrd() {
124 vtfile=$1; vtmagic=$2; vttmp=$3
125 echo "=====ventoy_unpack_initrd: #$*#" >> $VTLOG
127 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
128 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
129 echo "vtx=$vtx" >> $VTLOG
130 ${vtx:5} $vtfile > $vttmp
137 # This export is for busybox cpio command
138 export EXTRACT_UNSAFE_SYMLINKS=1
140 for vtfile in $(ls /initrd*); do
141 #decompress first initrd
142 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $vtfile)
144 if ventoy_is_initrd_ramdisk; then
145 ventoy_unpack_initrd $vtfile $vtmagic ${vtfile}_tmp
146 mv ${vtfile}_tmp $vtfile
149 ventoy_unpack_initramfs $vtfile 0 $vtmagic ${vtfile}_tmp
152 #only for cpio,cpio,...,initrd sequence, initrd,cpio or initrd,initrd sequence is not supported
153 while [ -e ${vtfile}_tmp ] && [ $(stat -c '%s' ${vtfile}_tmp) -gt 512 ]; do
154 mv ${vtfile}_tmp $vtfile
155 vtdump=$(hexdump -n 512 -e '512/1 "%02X"' $vtfile)
156 vtmagic=$(echo $vtdump | sed 's/^\(00\)*//')
157 let vtoffset="(${#vtdump}-${#vtmagic})/2"
159 if [ -z "$vtmagic" ]; then
160 echo "terminate with all zero data file" >> $VTLOG
164 ventoy_unpack_initramfs $vtfile $vtoffset ${vtmagic:0:4} ${vtfile}_tmp
167 rm -f $vtfile ${vtfile}_tmp
171 #break here for debug
172 if [ "$VTOY_BREAK_LEVEL" = "02" ] || [ "$VTOY_BREAK_LEVEL" = "12" ]; then
174 echo -e "\n\n\033[32m ################################################# \033[0m"
175 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
176 echo -e "\033[32m ################################################# \033[0m \n"
177 if [ "$VTOY_BREAK_LEVEL" = "12" ]; then
180 exec $BUSYBOX_PATH/sh
184 ####################################################################
186 # Step 3 : Extract injection archive #
188 ####################################################################
189 if [ -e $VTOY_PATH/ventoy_injection ]; then
190 echo "decompress injection ..." >>$VTLOG
192 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $VTOY_PATH/ventoy_injection)
193 echo "vtmagic=$vtmagic ..." >>$VTLOG
195 if [ "1F8B" = "vtmagic" ] || [ "1F9E" = "vtmagic" ]; then
196 zcat $VTOY_PATH/ventoy_injection | tar -xf -C /
197 elif [ "425A" = "vtmagic" ]; then
198 bzcat $VTOY_PATH/ventoy_injection | tar -xf -C /
199 elif [ "FD37" = "vtmagic" ]; then
200 xzcat $VTOY_PATH/ventoy_injection | tar -xf -C /
202 unzip -o -q $VTOY_PATH/ventoy_injection -d /
207 ####################################################################
209 # Step 4 : Hand over to ventoy.sh #
211 ####################################################################
212 echo "Now hand over to ventoy.sh" >>$VTLOG
213 . $VTOY_PATH/tool/vtoytool_install.sh
215 export PATH=$VTOY_ORG_PATH
216 exec $BUSYBOX_PATH/sh $VTOY_PATH/ventoy.sh