Dash:
[t2-trunk.git] / target / share / install / init
blob6572c5a8bd45431d71f1da0b1ea10c4171fcc92c
1 #!/bin/sh
3 # T2 SDE: target/share/install/init
4 # Partially based on linuxrc.c, Copyright (C) 2003, 2004 Clifford Wolf and Rene Rebe
5 # Converted from C to shell, heavily cleaned and improved for the T2 SDE by:
6 # (C)2005-2021 Rene Rebe, ExactCODE GmbH; Germany.
8 # Only print important stuff to console
9 # klogctl(8, NULL, 3);
11 doboot() {
12 if [ ! -e /sbin/init ]; then
13 echo "Can't find /sbin/init."
14 exit_linuxrc=0
17 if [ $exit_linuxrc = 1 ]; then
18 echo "finished. Continuing execution of second stage ..."
19 # we re-use all stage1 stuff, mounts, udev, you name it!
20 exec /sbin/init
24 trymount() {
25 # try best match / detected rootfs first, all the others thereafter
26 local filesystems=`disktype $1 2>/dev/null |
27 sed -e '/file system/!d' -e 's/file system.*//' -e 's/ //g' \
28 -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \
29 -e 's/fat32/vfat/'
30 sed '1!G ; $p ; h ; d' /proc/filesystems | sed /^nodev/d`
31 for fs in $filesystems; do
32 mount -t $fs -o ro $1 $2 && return
33 done
36 filter_for() {
37 case "$1" in
38 *.bz2) echo bunzip2 ;;
39 *.zst) echo unzstd ;;
40 *.gz) echo gunzip ;;
41 *) echo cat ;;
42 esac
45 select_stage2() {
46 # choices
47 case $1 in
48 http://*|ftp://*)
49 # the last sed is a uniq
50 images=`fget $1/ | sed 's/[<>"]/\n/g' | sed -n '/stage2/p' |
51 sed 'h;:b
53 N; /^\(.*\)\n\1$/ {g; bb
56 P; D'`
59 mkdir -p /media
60 trymount $1 /media
61 images=`cd /media; ls stage2*.tar*`
62 umount /media
64 esac
66 [ "$images" ] || return 1
68 mem=`sed -n '/MemTotal/{s/.* \([[:digit:]]*\) .*/\1/p}' /proc/meminfo`
69 if [ $mem -le 512000 ]; then
70 echo "Deleting /lib/firmware to save memory"
71 rm -rf /lib/firmware
74 echo "Select a second stage image file:"
75 i=1; default=$i
76 for x in $images; do
77 if [[ "$x" = *ext* ]]; then
78 [ $mem -gt 384000 ] && default=$i
79 else
80 [ $mem -le 384000 ] && default=$i
82 echo " $((i++)). $x"
83 done
84 [ $mem -le 64000 ] && autoload=0
85 echo -n "Enter number or image file names (default=$default): "
87 in=; [ $autoload = 0 ] && read in || echo
88 [ "$in" ] || in=$default
90 i=1
91 for x in $images; do
92 if [ "$((i++))" = "$in" ]; then
93 i=${x%.tar.*} # add base "stage2.tar.*"?
94 if [ "$i" != "stage2" ]; then
95 x="stage2.${x#stage2*.} $x"
97 eval $2=\"$x\"
98 return
100 done
101 eval $2=\"$in\"
104 httpload() {
105 baseurl=
106 filename=
107 url="$(net_gateway)"
108 url="http://${url:-server/path}/"
109 echo -en "\nEnter base URL (e.g. $url): "
111 read baseurl
112 baseurl="${baseurl:-$url}"
114 select_stage2 "$baseurl" filename
115 if [ -z "$filename" ]; then
116 echo "Failure retrieving the second stage image."
117 return
120 exit_init=1
121 export ROCK_INSTALL_SOURCE_URL=$baseurl
122 exit_linuxrc=1
124 echo "Extracting second stage filesystem ..."
125 filter=`filter_for "$filename"`
126 mv /bin/{,.}tar
127 for f in $filename; do
128 url=$baseurl/$f
129 if ! fget $url | $filter | (cd /; .tar x); then
130 echo "ERROR EXTRacting $f."
131 mv /bin/{.,}tar
132 exit_linuxrc=0
134 done
135 rm /bin/.tar
137 doboot
140 load_modules() {
141 echo "TODO: implement module loading shell"
144 getdevice() {
145 device=
146 devicelist=
148 # if no dev, wait for devices to appear, default Linux USB settle delay is 5s
149 local i=0; local default=
150 while [ -z "$devicelist" -a $i -le 6 ]; do
151 # with udev the above links are not created yet, so try to find
152 # device based on sysfs
153 local j=1
154 for x in /sys/block/*/device; do
155 [ -e "$x" ] || continue
156 x=${x#/sys/block/}; x=${x%/device}
157 [[ "$x" = fd* ]] && continue
158 local dev=/dev/$x; [ -L "$dev" ] && dev=$(readlink "$dev")
160 x=${x#/dev/}
161 local vendor="$(cat /sys/block/$x/device/vendor 2>/dev/null)"
162 local model="$(cat /sys/block/$x/device/model 2>/dev/null)"
164 # all partitions
165 local devs=`echo ${dev}*`
166 local parts=""
167 if [ "$devs" != "$dev" ]; then
168 parts=1
169 echo " $dev $vendor $model" # header
170 vendor=""
171 model=""
173 for d in $devs; do
174 if [ -z "$parts" -o "$d" != "$dev" ]; then
175 echo " $j. $d${vendor:+ $vendor $model}"
176 devicelist="$devicelist $d"
177 [ -z "$default" -a "$(< /sys/block/$x/removable)" == 1 ] && default=$j
178 : $((j++))
180 done
181 done
183 if [ -z "$devicelist" ]; then
184 echo "Waiting for devices to appear."
185 sleep 1
188 : $((i++))
189 done
191 if [ -z "$devicelist" ]; then
192 echo "No device found."
193 return
196 [ "$default" ] || default=1
197 echo -en "\nEnter number or device file name (default=$default): "
199 in=; [ $autoload = 0 ] && read in || echo
200 [ "$in" ] || in=$default
201 while [ -z "$device" ]; do
202 [ -z "$in" ] && return
203 if [ -e $in ]; then
204 device=$in
205 else
206 local i=1
207 for x in $devicelist; do
208 [ $in = $i ] && device=$x && break
209 : $((i++))
210 done
213 if [ -z "$device" ]; then
214 echo -n "No such device found. Try again (enter=back): "
215 read in
217 done
219 eval $1=$device
222 load_ramdisk_file() {
223 devicefile=
224 filename=
226 echo -e "\nSelect a device for loading the second stage system from:\n"
228 getdevice devicefile
229 if [ -z "$devicefile" ]; then
230 echo "No device detected."
231 return
234 select_stage2 $devicefile filename
235 if [ -z "$filename" ]; then
236 echo "No image found."
237 return
240 exit_linuxrc=1
241 echo "Using $devicefile:$filename"
243 mkdir -p /media || exit_linuxrc=0
244 trymount $devicefile /media || exit_linuxrc=0
246 echo "Extracting second stage filesystem ..."
247 filter=`filter_for "$filename"`
248 mv /bin/{,.}tar
249 for f in $filename; do
250 if ! $filter < /media/$f | (cd /; .tar x); then
251 echo "Error extracting $f."
252 mv /bin/{.,}tar
253 exit_linuxrc=0
255 done
257 umount /media || exit_linuxrc=0
259 export ROCK_INSTALL_SOURCE_DEV=$devicefile
260 export ROCK_INSTALL_SOURCE_FILE=$filename
261 doboot
264 activate_swap() {
265 echo -ne "\nEnter file name of swap device: "
267 read in
268 [ "$in" ] && swapon $in
271 net_config() {
272 echo -n "Interfaces: "; ls -A /sys/class/net/
274 echo -n "Enter interface name (eth0): "
275 read dev; [ "$dev" ] || dev="eth0"
277 echo -n "Enter IP address (auto): "
278 read ip
279 if [ "$ip" ]; then
280 echo -n "Enter default gateway (none): "
281 read gw
282 ipconfig $ip::$gw:::$dev:none
283 else
284 # let the link autoneg.
285 ipconfig :::::$dev:none > /dev/null 2>&1 ; sleep 4
286 echo "Auto configuration via DHCP, BOOTP, RARP ..."
287 ipconfig -d $dev -t 10
291 hex_to_ip() {
293 for i in $(echo "$1" | sed 's/\(..\)\(..\)\(..\)\(..\)/\4 \3 \2 \1/'); do
294 # pre- or append based on endianness
295 [ "$2" ] && ip="$((16#$i)).$ip" || ip="$ip$((16#$i))."
296 done
297 echo ${ip%.}
300 net_gateway() {
301 local gateway=
302 local endian=
303 while read if dst gw _ _ _ _ mask _; do
304 [ "$dst" = "00000000" ] && gateway=$gw
305 [[ $mask = FF*00 ]] && endian=big # guess little vs. big from mask
306 done < /proc/net/route
308 [ "$gateway" ] && hex_to_ip "$gateway" "$endian"
311 exec_sh() {
312 echo "Exit the shell to return to the 1st stage loader."
316 load_modules_disk() {
317 mkdir /mnt_floppy
319 trymount /dev/floppy/0 /mnt_floppy
320 load_modules /mnt_floppy
322 umount /mnt_floppy
323 rmdir /mnt_floppy
326 # try to auto-load from a disc, first
327 autoload=1
329 echo "
330 T2 SDE installer (1st stage - loader) ...
332 The T2 install system boots up in two stages. You are now in the first stage
333 and if everything goes right, you will not spend much time here. Just
334 configure the installation source so the second stage boot system can be
335 loaded and you can start the installation."
336 exit_init=0
337 while [ $exit_init = 0 ]; do
338 echo -n "
339 0. Load second stage system from local device
340 1. Load second stage system from network
341 2. Configure network interfaces
342 3. Load kernel modules from this disk
343 4. Load kernel modules from another disk
344 5. Activate already formatted swap device
345 6. Run shell (for experts!)
347 What do you want to do [0-8] (default=0)? "
349 in=; [ $autoload = 0 ] && read in || echo
350 [ "$in" ] || in=0
351 case $in in
352 0) load_ramdisk_file ;;
353 1) httpload ;;
354 2) net_config ;;
355 3) load_modules ;;
356 4) load_modules_disk ;;
357 5) activate_swap ;;
358 6) exec_sh ;;
359 *) echo "No such option." ;;
360 esac
361 # only try autoload once
362 autoload=0
363 done
365 exec /sbin/init /sbin/init
367 echo -e "\nPanic: Can't start /sbin/init!\n\n"