OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
8.3 KiB

  1. #!/bin/sh
  2. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # Filename: target/share/install/init
  6. # Copyright (C) 2006 - 2009 The OpenSDE Project
  7. # Copyright (C) 2006 The T2 SDE Project
  8. #
  9. # More information can be found in the files COPYING and README.
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; version 2 of the License. A copy of the
  14. # GNU General Public License can be found in the file COPYING.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. # partly based on linuxrc.c, Copyright (C) 2003, 2004 Cliford Wolf and Rene Rebe
  17. # converted from C to shell code and heavily cleaned and improved for the T2 SDE
  18. echo "OpenSDE early userspace ..."
  19. PATH=/sbin:/bin:/usr/bin:/usr/sbin
  20. echo "Mounting /dev, /proc and /sys ..."
  21. mount -t tmpfs none /dev
  22. mount -t proc none /proc
  23. mount -t usbfs none /proc/bus/usb
  24. mount -t sysfs none /sys
  25. ln -s /proc/self/fd /dev/fd
  26. if [ ! -c /dev/console ]; then
  27. mknod /dev/console c 5 1
  28. fi
  29. if [ ! -c /dev/null ]; then
  30. mknod /dev/null c 1 3
  31. fi
  32. if [ ! -c /dev/zero ]; then
  33. mknod /dev/zero c 1 5
  34. fi
  35. echo "Populating /dev (u/dev) ..."
  36. echo "" > /sys/kernel/uevent_helper
  37. udevd --daemon
  38. udevadm trigger
  39. udevadm settle
  40. # Only print important stuff to console
  41. # klogctl(8, NULL, 3);
  42. doboot()
  43. {
  44. if [ ! -e /mnt_root/sbin/init ]; then
  45. echo "Can't find /mnt_root/sbin/init."
  46. exit_linuxrc=0
  47. fi
  48. if [ $exit_linuxrc = 1 ]; then
  49. mount /dev /mnt_root/dev -t none -o move
  50. mount /proc /mnt_root/proc -t none -o move
  51. mount /sys /mnt_root/sys -t none -o move
  52. if [ ! -c /mnt_root/dev/console ]; then
  53. mknod /mnt_root/dev/console c 5 1
  54. fi
  55. if [ ! -c /mnt_root/dev/null ]; then
  56. mknod /mnt_root/dev/null c 1 3
  57. fi
  58. if [ ! -c /mnt_root/dev/zero ]; then
  59. mknod /mnt_root/dev/zero c 1 5
  60. fi
  61. # copy stuff only in the 1st_stage image
  62. mkdir -p /mnt_root/lib/modules
  63. cp -ar /lib/modules/* /mnt_root/lib/modules/
  64. # TODO: inject more first stage stuff if it was a small image
  65. # Before we switch to the real root we have to kill
  66. # the udevd we started in early userspace
  67. ls -1l /mnt_root/proc/*/exe 2> /dev/null | \
  68. sed -n -e "s,.*/proc/\([0-9]*\)/.*udevd.*,kill \1,p" | \
  69. /bin/sh
  70. exec switch_root /mnt_root /sbin/init
  71. else
  72. umount /mnt_root
  73. rmdir /mnt_root
  74. fi
  75. }
  76. trymount()
  77. {
  78. for fs in iso9660 ext3 ext2 minix vfat; do
  79. mount $1 $2 -t $fs -o ro && return
  80. done
  81. }
  82. select_2nd_stage()
  83. {
  84. # choices
  85. case $1 in
  86. http://*|ftp://*)
  87. # the last sed is a uniq
  88. images=`fget $1/ | sed 's/[<>"]/\n/g' | sed -n '/2nd_stage/p' |
  89. sed 'h;:b
  90. $b
  91. N; /^\(.*\)\n\1$/ {g; bb
  92. }
  93. $b
  94. P; D'`
  95. ;;
  96. *)
  97. mkdir /mnt_source
  98. trymount $1 /mnt_source
  99. images=`cd /mnt_source ; ls 2nd_stage*.tar*`
  100. umount /mnt_source; rmdir /mnt_source
  101. ;;
  102. esac
  103. [ "$images" ] || return 1
  104. echo "Select a second stage image file:"
  105. mem=`sed -n '/MemTotal/{s/.* \([[:digit:]]*\) .*/\1/p}' /proc/meminfo`
  106. i=1; default=$i
  107. for x in $images; do
  108. if [[ "$x" = *small* ]]; then
  109. [ $mem -le 96000 ] && default=$i
  110. else
  111. [ $mem -gt 96000 ] && default=$i
  112. fi
  113. echo " $(( i++ )). $x"
  114. done
  115. echo -n "Enter number or image file name (default=$default): "
  116. in=; [ $autoload = 0 ] && read in || echo
  117. [ "$in" ] || in=$default
  118. i=1
  119. for x in $images; do
  120. if [ "$(( i++ ))" = $in ]; then
  121. eval $2=$x
  122. return
  123. fi
  124. done
  125. }
  126. httpload()
  127. {
  128. baseurl=
  129. filename=
  130. url=
  131. echo -en "\nEnter base URL (e.g. http://1.2.3.4/t2): "
  132. read baseurl
  133. [ "$baseurl" ] || return
  134. select_2nd_stage "$baseurl" filename
  135. if [ -z "$filename" ]; then
  136. echo "Failure retrieving the second stage image."
  137. return
  138. fi
  139. exit_init=1
  140. url=$baseurl/$filename
  141. echo -e "[ $url ]\n"
  142. export ROCK_INSTALL_SOURCE_URL=$baseurl
  143. exit_linuxrc=1
  144. mkdir /mnt_root || exit_linuxrc=0
  145. mount none /mnt_root -t tmpfs || exit_linuxrc=0
  146. fget $url | ( cd /mnt_root; tar x ) # -z
  147. echo "finished ... continuing execution of second stage"
  148. doboot
  149. }
  150. load_modules()
  151. {
  152. echo "TODO: implement module loading shell"
  153. }
  154. getdevice()
  155. {
  156. device=
  157. devicelist=`ls /dev/cdrom* /dev/floppy* 2>/dev/null`
  158. # FIXME review and cleanup needed:
  159. if [ -z "$devicelist" ]; then
  160. # with udev, these links are not created yet, so try to find
  161. # device based on sysfs
  162. for sysname in /sys/*/*/removable; do
  163. [ `cat $sysname` = 1 ] || continue
  164. devname="`echo $sysname | sed -e 's,/removable,,' -e 's,.*/,,'`"
  165. # FIXME as udev is not running yet we are probably save
  166. # to assume that /dev/$devname exists, a check should be
  167. # performed however
  168. devicelist="$devicelist /dev/$devname"
  169. done
  170. if [ -z "$devicelist" ]; then
  171. echo "No device found."
  172. return
  173. fi
  174. fi
  175. i=1
  176. default=
  177. for dev in $devicelist ; do
  178. x=`readlink $dev`; [ "$x" ] && [[ "$x" != /dev/* ]] && x=/dev/$x
  179. [ "$x" ] && dev=$x
  180. case $dev in
  181. /dev/floppy*|/dev/fd*)
  182. echo " $i. $dev Floppy Disk Drive" ;;
  183. *)
  184. model=`cat /proc/ide/${dev#/dev/}/model 2>/dev/null`
  185. [ "$model" ] || model="SCSI or ATAPI CD-ROM"
  186. echo " $i. $dev $model"
  187. [ "$default" ] || default=$i
  188. ;;
  189. esac
  190. : $(( i++ ))
  191. done
  192. [ "$default" ] || default=1
  193. echo -en "\nEnter number or device file name (default=$default): "
  194. in=; [ $autoload = 0 ] && read in || echo
  195. [ "$in" ] || in=$default
  196. while [ -z "$device" ]; do
  197. [ -z "$in" ] && return
  198. if [ -e $in ]; then
  199. device=$in
  200. else
  201. i=1
  202. for x in $devicelist; do
  203. [ $in = $i ] && device=$x
  204. : $(( i++ ))
  205. done
  206. fi
  207. if [ -z "$device" ]; then
  208. echo -n "No such device found. Try again (enter=back): "
  209. read in
  210. fi
  211. done
  212. eval $1=$device
  213. }
  214. load_ramdisk_file()
  215. {
  216. devicefile=
  217. filename=
  218. echo -e "\nSelect a device for loading the second stage system from:\n"
  219. getdevice devicefile
  220. if [ -z "$devicefile" ]; then
  221. echo "No device detected."
  222. return
  223. fi
  224. select_2nd_stage $devicefile filename
  225. if [ -z "$filename" ]; then
  226. echo "No image found."
  227. return
  228. fi
  229. exit_linuxrc=1
  230. echo "Using $devicefile:$filename"
  231. mkdir /mnt_source || exit_linuxrc=0
  232. trymount $devicefile /mnt_source || exit_linuxrc=0
  233. mkdir /mnt_root || exit_linuxrc=0
  234. mount none /mnt_root -t tmpfs || exit_linuxrc=0
  235. echo "Extracting second stage filesystem ..."
  236. cat /mnt_source/$filename | ( cd /mnt_root; tar x ) || # -z
  237. echo "Can't run tar on $fielname."
  238. umount /mnt_source || exit_linuxrc=0
  239. rmdir /mnt_source || exit_linuxrc=0
  240. export ROCK_INSTALL_SOURCE_DEV=$devicefile
  241. export ROCK_INSTALL_SOURCE_FILE=$filename
  242. doboot
  243. }
  244. activate_swap()
  245. {
  246. echo -ne "\nEnter file name of swap device: "
  247. read in
  248. [ "$in" ] && swapon $in
  249. }
  250. net_config()
  251. {
  252. echo -n "Interfaces: "; ls -A /sys/class/net/
  253. echo -n "Enter interface name (eth0): "
  254. read dev ; [ "$dev" ] || dev="eth0"
  255. echo -n "Enter IP address (auto): "
  256. read ip
  257. if [ "$ip" ]; then
  258. echo -n "Enter default gateway (none): "
  259. read gw
  260. ipconfig $ip::$gw:::$dev:none
  261. else
  262. # let the link autoneg.
  263. ipconfig :::::$dev:none > /dev/null 2>&1 ; sleep 4
  264. echo "Auto configuration via DHCP, BOOTP, RARP ..."
  265. ipconfig -d $dev -t 10
  266. fi
  267. }
  268. exec_sh()
  269. {
  270. echo "Exit the shell to return to the 1st stage loader."
  271. sh
  272. }
  273. load_modules_disk ()
  274. {
  275. mkdir /mnt_floppy
  276. trymount /dev/floppy/0 /mnt_floppy
  277. load_modules /mnt_floppy
  278. umount /mnt_floppy
  279. rmdir /mnt_floppy
  280. }
  281. # try to auto-load from a disc, first
  282. autoload=1
  283. echo "
  284. OpenSDE installer (1st stage - loader) ...
  285. The OpenSDE install system boots up in two stages. You are now in the first
  286. stage and if everything goes right, you will not spend much time here.
  287. Just configure the installation source so the second stage boot system can
  288. be loaded and you can start the installation."
  289. exit_init=0
  290. while [ $exit_init = 0 ]; do
  291. echo -n "
  292. 0. Load second stage system from local device
  293. 1. Load second stage system from network
  294. 2. Configure network interfaces
  295. 3. Load kernel modules from this disk
  296. 4. Load kernel modules from another disk
  297. 5. Activate already formatted swap device
  298. 6. Run shell (for experts!)
  299. What do you want to do [0-8] (default=0)? "
  300. in=; [ $autoload = 0 ] && read in || echo
  301. [ "$in" ] || in=0
  302. case $in in
  303. 0) load_ramdisk_file ;;
  304. 1) httpload ;;
  305. 2) net_config ;;
  306. 3) load_modules ;;
  307. 4) load_modules_disk ;;
  308. 5) activate_swap ;;
  309. 6) exec_sh ;;
  310. *) echo "No such option." ;;
  311. esac
  312. # only try autoload once
  313. autoload=0
  314. done
  315. exec /sbin/init /sbin/init
  316. echo -e "\nPanic: Can't start /sbin/init!\n\n"