slitaz-arm rev 209

piboot: non-kexec kernels support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Mar 25 15:13:32 2016 +0100 (2016-03-25)
parents 23f85a7e62b1
children 5be8b2141f10
files rpi/piboot
line diff
     1.1 --- a/rpi/piboot	Wed Mar 23 10:38:14 2016 +0100
     1.2 +++ b/rpi/piboot	Fri Mar 25 15:13:32 2016 +0100
     1.3 @@ -10,21 +10,16 @@
     1.4  # AUTHORS: Pascal Bellard <pascal.bellard@slitaz.org>
     1.5  #
     1.6  
     1.7 -# Check for depends ...
     1.8 -LOG=/var/log/$(basename $0).log
     1.9 -mv -f $LOG $LOG.0 2>/dev/null
    1.10 -while read file pkg ; do
    1.11 -	[ "$(which $file)" ] && continue
    1.12 -	echo "Can't find $file. Please install $pkg." >> $LOG
    1.13 -done <<EOT
    1.14 -kexec	kexec-tools
    1.15 -dialog	dialog
    1.16 -EOT
    1.17 -[ -s $LOG ] && exec /init
    1.18 +[ -z "$(which dialog)" ] && echo "Please install dialog." && exec /init
    1.19  
    1.20  cmdlinearg()
    1.21  {
    1.22 -	grep -q $1= /proc/cmdline && sed "s/.*$1=\([^ ]*\).*/\1/" </proc/cmdline
    1.23 +	sed "/$1=/!d;s/.*$1=\([^ ]*\).*/\1/" < /proc/cmdline
    1.24 +}
    1.25 +
    1.26 +cmdvararg()
    1.27 +{
    1.28 +	echo " $CMDLINE" | sed "/ $1=/!d;s/.* $1=\([^ ]*\).*/\1/"
    1.29  }
    1.30  
    1.31  get()
    1.32 @@ -61,7 +56,7 @@
    1.33  		--ok-label "${msgedit:-Edit}" \
    1.34  		--cancel-label "${msgdone:-Done}" \
    1.35  		$extra \
    1.36 -		--menu "" 20 72 14 \
    1.37 +		--menu "$msgmenu" 20 72 14 \
    1.38  		Kernel "$KERNEL" \
    1.39  		Initramfs "$INITRD" \
    1.40  		Cmdline "$CMDLINE" 2>&1 1>&3 )
    1.41 @@ -71,11 +66,12 @@
    1.42  	case $retval in
    1.43  	1|255) return ;;
    1.44  	3)	cp $mnt/$KEYWORD/config.txt /tmp/config.txt
    1.45 -		sed -i '/^kernel/d;/^initrd/d;/^cmdline/d' /tmp/config.txt
    1.46 +		sed -i '/^kernel\|^initrd/d' /tmp/config.txt
    1.47  		echo "kernel=$KERNEL" >> /tmp/config.txt
    1.48  		[ "$INITRD" ] && echo "initrd $INITRD" >> /tmp/config.txt
    1.49 -		[ "$CMDLINE" ] && echo "cmdline=$CMDLINE" >> /tmp/config.txt
    1.50  		mount -o remount,rw $mnt
    1.51 +		[ "$CMDLINE" ] && echo "$CMDLINE" > $mnt/$KEYWORD/cmdline.txt ||
    1.52 +		rm -f $mnt/$KEYWORD/cmdline.txt
    1.53  		cp /tmp/config.txt $mnt/$KEYWORD/config.txt
    1.54  		mount -o remount,ro $mnt ;;
    1.55  	esac
    1.56 @@ -134,13 +130,17 @@
    1.57  
    1.58  web_get()
    1.59  {
    1.60 +	if [ -z "$webprefix" ]; then
    1.61 +		cat $mnt/$1 >> $2
    1.62 +		return
    1.63 +	fi
    1.64  	case "$1" in
    1.65  	*torrent) # See http://sl-lab.it/dokuwiki/doku.php/tesi:boottorrent_en
    1.66  		[ -z "$(which aria2)" ] &&
    1.67  			echo "Can't find aria2." >> $LOG &&
    1.68  			exec /init
    1.69  
    1.70 -		wget -O /root/webboot.torrent "$1"
    1.71 +		wget -O /root/webboot.torrent "$webprefix/$1"
    1.72  		aria2c --enable-dht=false --disable-ipv6=true --seed-time=0 \
    1.73  			--file-allocation=none -j5 /root/webboot.torrent
    1.74  
    1.75 @@ -153,7 +153,7 @@
    1.76  		fi
    1.77  		rm -f /root/webboot.torrent ;;
    1.78  	*)
    1.79 -		wget -O - "$1" >> $2
    1.80 +		wget -O - "$webprefix/$1" >> $2
    1.81  	esac
    1.82  }
    1.83  
    1.84 @@ -196,6 +196,7 @@
    1.85  
    1.86  msgsave="$(get msgsave $mnt/menu.txt)"
    1.87  msgedit="$(get msgedit $mnt/menu.txt)"
    1.88 +msgmenu="$(get msgmenu $mnt/menu.txt)"
    1.89  msgdone="$(get msgdone $mnt/menu.txt)"
    1.90  msgboot="$(get msgboot $mnt/menu.txt)"
    1.91  msghalt="$(get msghalt $mnt/menu.txt)"
    1.92 @@ -208,14 +209,14 @@
    1.93  
    1.94  n=0
    1.95  for i in $mnt/*/ ; do
    1.96 +	[ -s "$i/menu.txt" ] || continue
    1.97  	KERNEL="$(get kernel $i/config.txt)"
    1.98 -	[ -z "$KERNEL" ] && [ -s $i/kernel.img ] && KERNEL="kernel.img"
    1.99 -	[ -z "$KERNEL" ] && [ -s $i/menu.txt ] && KERNEL="../kernel.img"
   1.100 -	[ "$KERNEL" ] || continue
   1.101 +	[ -z "$KERNEL" ] && [ -s "$i/kernel.img" ] && KERNEL="kernel.img"
   1.102 +	[ -z "$KERNEL" ] && [ -s "$i/cmdline.txt" ] && KERNEL="../kernel.img"
   1.103  	n=$(($n+1))
   1.104  	eval KERNEL_$n="\$KERNEL"
   1.105  	eval INITRD_$n="\$(get initramfs \$i/config.txt)"
   1.106 -	eval CMDLINE_$n="\$(cat \$i/cmdline.txt)"
   1.107 +	eval CMDLINE_$n="\$(cat \$i/cmdline.txt 2> /dev/null)"
   1.108  	eval SAVEDEFAULT_$n="\$(get savedefault \$i/config.txt)"
   1.109  	eval KEYWORD_$n="\$(basename \$i)"
   1.110  	eval ENTRY_$n="\$(get entry \$i/menu.txt)"
   1.111 @@ -250,7 +251,7 @@
   1.112  		[ "$KEYWORD" ] || break
   1.113  		[ "$KEYWORD" == "$value" ] && break
   1.114  		case $value in
   1.115 -		*timeout)	[ "$KEYWORD" == "$default" ] && break ;;
   1.116 +		*timeout)	[ "$KEYWORD" == "${default:-$KEYWORD}" ] && break ;;
   1.117  		esac
   1.118  	done
   1.119  	
   1.120 @@ -276,11 +277,10 @@
   1.121  
   1.122  # Start OS
   1.123  
   1.124 +KEXEC=
   1.125  case "$KEYWORD" in
   1.126  
   1.127 -''|Continue)
   1.128 -	KEXEC= ;;
   1.129 -
   1.130 +''|Continue)	;;
   1.131  *)
   1.132  	eval KERNEL="\$KERNEL_$n"
   1.133  	eval INITRD="\$INITRD_$n"
   1.134 @@ -292,29 +292,6 @@
   1.135  	*)	KERNEL=${KERNEL/.img/7.img}
   1.136  	esac
   1.137  
   1.138 -	if [ "$webprefix" ]; then
   1.139 -		cd /root
   1.140 -		web_get $webprefix/$KEYWORD/$KERNEL vmlinuz
   1.141 -		KERNEL=vmlinuz
   1.142 -		for i in ${INITRD//,/ }; do
   1.143 -			web_get $webprefix/$KEYWORD/$i rootfs.gz
   1.144 -			n=$(stat -c %s rootfs.gz)
   1.145 -			n=$(($n % 4))
   1.146 -			[ $n -eq 0 ] && continue
   1.147 -			dd if=/dev/zero bs=1 count=$((4 - $n)) >> rootfs.gz
   1.148 -		done
   1.149 -	else
   1.150 -		cd $mnt/$KEYWORD
   1.151 -		cp $KERNEL /root
   1.152 -		for i in ${INITRD//,/ }; do
   1.153 -			cat $i >> /root/rootfs.gz
   1.154 -			n=$(stat -c %s /root/rootfs.gz)
   1.155 -			n=$(($n % 4))
   1.156 -			[ $n -eq 0 ] && continue
   1.157 -			dd if=/dev/zero bs=1 count=$((4 - $n)) >> /root/rootfs.gz
   1.158 -		done
   1.159 -		cd /root
   1.160 -	fi
   1.161  	if [ -z "$READONLY" ] && [ "$SAVEDEFAULT" ] &&
   1.162  	   [ -s $mnt/$SAVEDEFAULT/config.txt ]; then
   1.163  	   	cp $mnt/menu.txt /tmp
   1.164 @@ -323,15 +300,52 @@
   1.165  		mount -o remount,rw $mnt
   1.166  		cp /tmp/menu.txt $mnt/menu.txt
   1.167  	fi
   1.168 -	[ "$INITRD" ] && INITRD="--initrd rootfs.gz"
   1.169  
   1.170 -	kexec -l $(basename $KERNEL) $INITRD --command-line \
   1.171 -		"$(sed 's|  .*||' /proc/cmdline)  $CMDLINE" --atags
   1.172 -	KEXEC="kexec -e" ;;
   1.173 +	for i in ${INITRD//,/ }; do
   1.174 +		web_get $KEYWORD/$i /rootfs
   1.175 +		if ! kexec -u; then
   1.176 +			( zcat /rootfs || unlzma < /rootfs ) | cpio -idmu
   1.177 +			rm -f /rootfs
   1.178 +		else
   1.179 +			n=$(stat -c %s /rootfs)
   1.180 +			n=$(($n % 4))
   1.181 +			[ $n -ne 0 ] &&
   1.182 +			dd if=/dev/zero bs=1 count=$((4 - $n)) >> /rootfs
   1.183 +		fi 2> /dev/null
   1.184 +	done
   1.185  
   1.186 +	if ! kexec -u 2> /dev/null; then
   1.187 +		ROOTDIR=$(cmdvararg "subroot")
   1.188 +		TMPDIR=$ROOTDIR/dev/shm
   1.189 +		TMPDIR=${TMPDIR#/}
   1.190 +		ROOTDEV=$(cmdvararg "root")
   1.191 +		[ "$ROOTDEV" == "/dev/null" ] && ROOTDEV=$(cmdvararg "mount")
   1.192 +		K=$(cmdvararg "kmap")
   1.193 +		[ "$K" ] && echo $K > /etc/keymap.conf
   1.194 +		L=$(cmdvararg "lang")
   1.195 +		[ "$L" ] && echo -e "LANG=$L\nLC_ALL=$L" > /etc/locale.conf
   1.196 +		TZ=$(cmdvararg "tz")
   1.197 +		[ "$TZ" ] && echo $TZ > /etc/TZ
   1.198 +		umount $mnt
   1.199 +		if [ "$ROOTDEV" ] && mount $ROOTDEV $mnt &&
   1.200 +		   mount -t tmpfs tmpfs $mnt/$TMPDIR ; then
   1.201 +			CHROOT=/usr/sbin/chroot
   1.202 +			cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $CHROOT \
   1.203 +			     | sed 's|.*=> \(/lib/l[^ ]*\).*|\1|;/^\//!d') \
   1.204 +				$CHROOT $mnt/$TMPDIR
   1.205 +			CHROOT="$(cd $mnt ; ls $TMPDIR/ld-*so) $TMPDIR/chroot"
   1.206 +			export LD_LIBRARY_PATH=/$TMPDIR:/lib
   1.207 +			KEXEC="exec /sbin/switch_root $mnt $CHROOT $ROOTDIR/ /sbin/init"
   1.208 +		fi
   1.209 +	else
   1.210 +		web_get $KEYWORD/$KERNEL /vmlinuz
   1.211 +		umount $mnt
   1.212 +		kexec -l /vmlinuz ${INITRD+=--initrd /rootfs} --command-line \
   1.213 +			"$(sed 's|  .*||' /proc/cmdline) $CMDLINE" --atags
   1.214 +		KEXEC="kexec -e"
   1.215 +	fi ;;
   1.216  esac
   1.217  
   1.218 -umount $mnt
   1.219  umount /sys
   1.220  umount /proc
   1.221  $KEXEC