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 /sbin /dev/ /root
56 ventoy_is_initrd_ramdisk() {
57 #As I known, PCLinuxOS use ramdisk
58 if echo $vtkerver | grep -i -q 'PCLinuxOS'; then
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
71 #if [ "${vtmagic:0:4}" = '5678' ]; then
72 # echo -en '\x1F\x8B' | dd status=none of=$vtfile bs=1 count=2 conv=notrunc
76 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
77 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
78 echo "vtx=$vtx" >> $VTLOG
79 if [ $vtskip -eq 0 ]; then
80 if [ "${vtx:5}" = "xzcat" ]; then
81 rm -f $VTOY_PATH/xzlog
82 ${vtx:5} $vtfile 2> $VTOY_PATH/xzlog | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
84 if grep -q 'corrupted data' $VTOY_PATH/xzlog; then
85 echo 'xzcat failed, now try xzminidec...' >> $VTLOG
86 cat $vtfile | xzminidec | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
90 ${vtx:5} $vtfile | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
93 dd if=$vtfile skip=$vtskip iflag=skip_bytes status=none | ${vtx:5} | (cpio -idmu 2>>$VTLOG; cat > $vttmp)
100 # param: file magic tmp
101 ventoy_unpack_initrd() {
102 vtfile=$1; vtmagic=$2; vttmp=$3
103 echo "=====ventoy_unpack_initrd: #$*#" >> $VTLOG
105 for vtx in '1F8B zcat' '1F9E zcat' '425A bzcat' '5D00 lzcat' 'FD37 xzcat' '894C lzopcat' '0221 lz4cat' '28B5 zstdcat' '3037 cat'; do
106 if [ "${vtx:0:4}" = "${vtmagic:0:4}" ]; then
107 echo "vtx=$vtx" >> $VTLOG
108 ${vtx:5} $vtfile > $vttmp
115 # This export is for busybox cpio command
116 export EXTRACT_UNSAFE_SYMLINKS=1
118 for vtfile in $(ls /initrd*); do
119 #decompress first initrd
120 vtmagic=$(hexdump -n 2 -e '2/1 "%02X"' $vtfile)
122 if ventoy_is_initrd_ramdisk; then
123 ventoy_unpack_initrd $vtfile $vtmagic ${vtfile}_tmp
124 mv ${vtfile}_tmp $vtfile
127 ventoy_unpack_initramfs $vtfile 0 $vtmagic ${vtfile}_tmp
130 #only for cpio,cpio,...,initrd sequence, initrd,cpio or initrd,initrd sequence is not supported
131 while [ -e ${vtfile}_tmp ] && [ $(stat -c '%s' ${vtfile}_tmp) -gt 512 ]; do
132 mv ${vtfile}_tmp $vtfile
133 vtdump=$(hexdump -n 512 -e '512/1 "%02X"' $vtfile)
134 vtmagic=$(echo $vtdump | sed 's/^\(00\)*//')
135 let vtoffset="(${#vtdump}-${#vtmagic})/2"
137 if [ -z "$vtmagic" ]; then
138 echo "terminate with all zero data file" >> $VTLOG
142 ventoy_unpack_initramfs $vtfile $vtoffset ${vtmagic:0:4} ${vtfile}_tmp
145 rm -f $vtfile ${vtfile}_tmp
149 #break here for debug
150 if [ "$VTOY_BREAK_LEVEL" = "02" ] || [ "$VTOY_BREAK_LEVEL" = "12" ]; then
152 echo -e "\n\n\033[32m ################################################# \033[0m"
153 echo -e "\033[32m ################ VENTOY DEBUG ################### \033[0m"
154 echo -e "\033[32m ################################################# \033[0m \n"
155 if [ "$VTOY_BREAK_LEVEL" = "12" ]; then
158 exec $BUSYBOX_PATH/sh
162 ####################################################################
164 # Step 3 : Hand over to ventoy.sh #
166 ####################################################################
167 echo "Now hand over to ventoy.sh" >>$VTLOG
168 . $VTOY_PATH/tool/vtoytool_install.sh
170 export PATH=$VTOY_ORG_PATH
171 exec $BUSYBOX_PATH/sh $VTOY_PATH/ventoy.sh