mirror of the now-defunct rocklinux.org
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.

465 lines
10 KiB

  1. #!/bin/bash
  2. STAGE_2_BIG_IMAGE="2nd_stage.tar.gz"
  3. STAGE_2_SMALL_IMAGE="2nd_stage_small.tar.gz"
  4. STAGE_2_COMPRESS_ARG="--use-compress-program=gzip"
  5. #640kB, err, 64 MB should be enought for the tmpfs ;-)
  6. TMPFS_OPTIONS="size=67108864"
  7. mod_load_info () { # {{{
  8. read os host version rest < <( uname -a )
  9. if [ -z "${os}" ] ; then
  10. echo "Can't run \`uname -a\`"
  11. return
  12. elif [ "${os}" != "Linux" ] ; then
  13. echo "Your operating system is not supported ?!"
  14. return
  15. fi
  16. mod_loader="/sbin/insmod"
  17. mod_dir="/lib/modules/"
  18. # kernel module suffix for <= 2.4 is .o, .ko if above
  19. if [ ${version:2:1} -gt 4 ] ; then
  20. mod_suffix=".ko"
  21. mod_suffix_len=3
  22. else
  23. mod_suffix=".o"
  24. mod_suffix_len=2
  25. fi
  26. } # }}}
  27. doboot() { # {{{
  28. if ! mkdir /mnt_root/old_root ; then
  29. echo "Can't create /mnt_root/old_root"
  30. exit_linuxrc=0
  31. fi
  32. if [ ! -f /mnt_root/linuxrc ] ; then
  33. echo "Can't find /mnt_root/linuxrc!"
  34. exit_linuxrc=0
  35. fi
  36. if [ ${exit_linuxrc} -ne 0 ] ; then
  37. if ! pivot_root "/mnt_root" "/mnt_root/old_root" ; then
  38. echo "Can't call pivot_root"
  39. exit_linuxrc=0
  40. return
  41. fi
  42. cd /
  43. if ! mount --move /old_root/dev /dev ; then
  44. echo "Can't remount /old_root/dev as /dev"
  45. fi
  46. if ! mount --move /old_root/proc /proc ; then
  47. echo "Can't remount /old_root/proc as /proc"
  48. fi
  49. if ! mount --move /old_root/sys /sys ; then
  50. echo "Can't remount /old_root/sys as /sys"
  51. fi
  52. if ! umount /old_root/tmp ; then
  53. echo "Can't umount /old_root/tmp"
  54. fi
  55. else
  56. rmdir /mnt_root/old_root || echo "Can't remove /mnt_root/old_root"
  57. umount /mnt_root || echo "Can't umount /mnt_root"
  58. rmdir /mnt_root || echo "Can't remove /mnt_root"
  59. fi
  60. } # }}}
  61. trymount() { # {{{
  62. source=${1}
  63. target=${2}
  64. mount -t iso9600 -o ro ${source} ${target} && return 0
  65. mount -t ext3 -o ro ${source} ${target} && return 0
  66. mount -t ext2 -o ro ${source} ${target} && return 0
  67. mount -t minix -o ro ${source} ${target} && return 0
  68. mount -t vfat -o ro ${source} ${target} && return 0
  69. return -1
  70. } # }}}
  71. httpload() { # {{{
  72. echo -n "Enter base URL (e.g. http://1.2.3.4/rock): "
  73. read baseurl
  74. [ -z "${baseurl}" ] && return
  75. cat <<EOF
  76. Select a stage 2 image file:
  77. 0. ${STAGE_2_BIG_IMAGE}
  78. 1. ${STAGE_2_SMALL_IMAGE}
  79. EOF
  80. echo -n "Enter number or image file name (default=0): "
  81. read filename
  82. if [ -z "${filename}" ] ; then
  83. filename="${STAGE_2_BIG_IMAGE}"
  84. elif [ "${filename}" == "0" ] ; then
  85. filename=${STAGE_2_BIG_IMAGE}
  86. elif [ "${filename}" == "1" ] ; then
  87. filename="${STAGE_2_SMALL_IMAGE}"
  88. fi
  89. url="${baseurl%/}/${filename}"
  90. echo "[ ${url} ]"
  91. ROCK_INSTALL_SOURCE_URL=${baseurl}
  92. exit_linuxrc=1;
  93. if ! mkdir /mnt_root ; then
  94. echo "Can't create /mnt_root"
  95. exit_linuxrc=0
  96. fi
  97. if ! mount -t tmpfs -O ${TMPFS_OPTIONS} none /mnt_root ; then
  98. echo "Can't mount /mnt_root"
  99. exit_linuxrc=0
  100. fi
  101. wget -O - ${url} | tar ${STAGE_2_COMPRESS_ARG} -C /mnt_root -xf -
  102. echo "finished ... now booting 2nd stage"
  103. doboot
  104. } # }}}
  105. load_modules() { # {{{
  106. # this starts the module loading shell
  107. directory=${1}
  108. cat <<EOF
  109. module loading shell
  110. you can navigate through the filestem with 'cd'. for loading a module
  111. simply enter the shown name, to exit press enter on a blank line.
  112. EOF
  113. cd ${directory}
  114. while : ; do
  115. echo "Directories:"
  116. count=0
  117. while read inode ; do
  118. [ -d "${inode}" ] || continue
  119. echo -n " [ ${inode} ]"
  120. count=$((${count}+1))
  121. if [ ${count} -gt 3 ] ; then
  122. echo
  123. count=0
  124. fi
  125. done < <( ls ) | expand -t1,3,19,21,23,39,41,43,59,61,63,78
  126. echo
  127. echo "Modules:"
  128. count=0
  129. while read inode ; do
  130. [ -f "${inode}" ] || continue
  131. [ "${inode%${mod_suffix}}" == "${inode}" ] && continue
  132. echo -n " [ ${inode%${mod_suffix}} ]"
  133. count=$((${count}+1))
  134. if [ ${count} -gt 3 ] ; then
  135. echo
  136. count=0
  137. fi
  138. done < <( ls ) | expand -t1,3,19,21,23,39,41,43,59,61,63,78
  139. echo
  140. echo -n "[${PWD##*/} ] > "
  141. read cmd par
  142. if [ "${cmd}" == "cd" ] ; then
  143. cd ${par}
  144. elif [ -f "${cmd}${mod_suffix}" ] ; then
  145. insmod ${PWD%/}/${cmd}${mod_suffix} ${par}
  146. elif [ -z "${cmd}" ] ; then
  147. break
  148. else
  149. echo "No such module: ${cmd}"
  150. fi
  151. done
  152. return
  153. } # }}}
  154. getdevice () { # {{{
  155. cdroms="${1}"
  156. floppies="${2}"
  157. autoboot="${3}"
  158. devicelists="/dev/cdroms/* /dev/floppy/*"
  159. [ "${cdroms}" == "0" -a "${floppies}" == "0" ] && return -1
  160. devnr=0
  161. for dev in ${devicelists} ; do
  162. [ -e "${dev}" ] || continue
  163. [[ ${dev} = /dev/cdroms* ]] && [ "${cdroms}" == "0" ] && continue
  164. [[ ${dev} = /dev/floppy* ]] && [ "${floppies}" == "0" ] && continue
  165. eval "device_${devnr}='${dev}'"
  166. devnr=$((${devnr}+1))
  167. done
  168. [ ${devnr} -eq 0 ] && return -1
  169. x=0
  170. floppy=1
  171. cdrom=1
  172. while [ ${x} -lt ${devnr} ] ; do
  173. eval "device=\${device_${x}}"
  174. if [[ ${device} = /dev/cdrom* ]] ; then
  175. echo " ${x}. CD-ROM #${cdrom} (IDE/ATAPI or SCSI)"
  176. cdrom=$((${cdrom}+1))
  177. fi
  178. if [[ ${device} = /dev/flopp* ]] ; then
  179. echo " ${x}. FDD (Floppy Disk Drive) #${floppy}"
  180. floppy=$((${floppy}+1))
  181. fi
  182. x=$((${x}+1))
  183. done
  184. echo -en "\nEnter number or device file name (default=0): "
  185. if [ ${autoboot} -eq 1 ] ; then
  186. echo "0"
  187. text=0
  188. else
  189. read text
  190. fi
  191. [ -z "${text}" ] && text=0
  192. while : ; do
  193. if [ -e "${text}" ] ; then
  194. devicefile="${text}"
  195. return 0
  196. fi
  197. eval "text=\"\${device_${text}}\""
  198. if [ -n "${text}" ] ; then
  199. devicefile="${text}"
  200. return 0
  201. fi
  202. echo -n "No such device found. Try again (enter=back): "
  203. read text
  204. [ -z "${text}" ] && return -1
  205. done
  206. return 1;
  207. } # }}}
  208. load_ramdisk_file() { # {{{
  209. autoboot=${1}
  210. echo -en "Select a device for loading the 2nd stage system from: \n\n"
  211. getdevice 1 1 ${autoboot} || return
  212. cat << EOF
  213. Select a stage 2 image file:
  214. 1. ${STAGE_2_BIG_IMAGE}
  215. 2. ${STAGE_2_SMALL_IMAGE}
  216. EOF
  217. echo -n "Enter number or image file name (default=1): "
  218. if [ ${autoboot} -eq 1 ] ; then
  219. echo "1"
  220. text=1
  221. else
  222. read text
  223. fi
  224. if [ -z "${text}" ] ; then
  225. filename="${STAGE_2_BIG_IMAGE}"
  226. elif [ "${text}" == "1" ] ; then
  227. filename="${STAGE_2_BIG_IMAGE}"
  228. elif [ "${text}" == "2" ] ; then
  229. filename="${STAGE_2_SMALL_IMAGE}"
  230. else
  231. filename="${text}"
  232. fi
  233. exit_linuxrc=1
  234. echo "Using ${devicefile}:${filename}."
  235. if ! mkdir -p /mnt_source ; then
  236. echo "Can't create /mnt_source"
  237. exit_linuxrc=0
  238. fi
  239. if ! mount ${devicefile} "/mnt_source" ; then
  240. echo "Can't mount /mnt_source"
  241. exit_linuxrc=0
  242. fi
  243. if ! mkdir -p /mnt_root ; then
  244. echo "Can't create /mnt_root"
  245. exit_linuxrc=0
  246. fi
  247. if ! mount -t tmpfs -o ${TMPFS_OPTIONS} none /mnt_root ; then
  248. echo "Can't mount tmpfs on /mnt_root"
  249. exit_linuxrc=0
  250. fi
  251. echo "Extracting 2nd stage filesystem to ram ..."
  252. if ! tar ${STAGE_2_COMPRESS_ARG} -C /mnt_root -xf /mnt_source/${filename} ; then
  253. echo "Can't extract /mnt/source/${filename}"
  254. exit_linuxrc=0
  255. return 1
  256. fi
  257. if ! umount "/mnt_source" ; then
  258. echo "Can't umount /mnt_source"
  259. exit_linuxrc=0
  260. fi
  261. if ! rmdir "/mnt_source" ; then
  262. echo "Can't remove /mnt_source"
  263. exit_linuxrc=0
  264. fi
  265. ROCK_INSTALL_SOURCE_DEV=${devicefile}
  266. ROCK_INSTALL_SOURCE_FILE=${filename}
  267. doboot
  268. } # }}}
  269. activate_swap() { # {{{
  270. echo
  271. echo -n "Enter file name of swap device: "
  272. read text
  273. if [ -n "${text}" ] ; then
  274. swapon ${text}
  275. fi
  276. } # }}}
  277. config_net() { # {{{
  278. ip addr
  279. echo
  280. ip route
  281. echo
  282. echo -n "Enter interface name (eth0): "
  283. read dv
  284. [ -z "${dv}" ] && dv="eth0"
  285. echo -n "Enter ip (192.168.0.254/24): "
  286. read ip
  287. [ -z "${ip}" ] && ip="192.168.0.254/24"
  288. ip addr add ${ip} dev ${dv}
  289. ip link set ${dv} up
  290. echo -n "Enter default gateway (none): "
  291. read gw
  292. [ -n "${gw}" ] && ip route add default via ${gw}
  293. ip addr
  294. echo
  295. ip route
  296. echo
  297. } # }}}
  298. autoload_modules () { # {{{
  299. while read cmd mod rest ; do
  300. [ -n "${rest}" ] && continue
  301. [ -z "${cmd}" ] && continue
  302. if [ "${cmd}" == "modprobe" -o "${cmd}" == "insmod" ] ; then
  303. echo "${cmd} ${mod}"
  304. ${cmd} ${mod} 2>&1 >/dev/null
  305. fi
  306. done < <( /bin/gawk -f /bin/hwscan )
  307. } # }}}
  308. exec_sh() { # {{{
  309. echo "Quit the shell to return to the stage 1 loader!"
  310. /bin/sh
  311. } # }}}
  312. checkisomd5() { # {{{
  313. echo "Select a device for checking: "
  314. getdevice 1 0 0 || return
  315. echo "Running check..."
  316. /bin/checkisomd5 ${devicefile}
  317. echo "done"
  318. echo "Press Return key to continue."
  319. read
  320. } # }}}
  321. input=1
  322. exit_linuxrc=0
  323. [ -z "${autoboot}" ] && autoboot=0
  324. mount -t ramfs none /dev || echo "Can't mount a ramfs on /dev"
  325. mount -t sysfs none /sys || echo "Can't mount sysfs on /sys"
  326. mount -t proc none /proc || echo "Can't mount /proc"
  327. mount -t tmpfs -o ${TMPFS_OPTIONS} none /tmp || echo "Can't mount /tmpfs"
  328. udevstart
  329. cd /dev
  330. rm -rf fd
  331. ln -s /proc/self/fd
  332. cd -
  333. mod_load_info
  334. autoload_modules
  335. if [ ${autoboot} -eq 1 ] ; then
  336. load_ramdisk_file 1
  337. fi
  338. autoboot=0
  339. cat << EOF
  340. ============================================
  341. === ROCK Linux 1st stage boot system ===
  342. ============================================
  343. The ROCK Linux install / rescue system boots up in two stages. You
  344. are now in the first of this two stages and if everything goes right
  345. you will not spend much time here. Just load your SCSI and networking
  346. drivers (if needed) and configure the installation source so the
  347. 2nd stage boot system can be loaded and you can start the installation.
  348. EOF
  349. while [ ${exit_linuxrc} -eq 0 ] ; do
  350. cat <<EOF
  351. 0. Load 2nd stage system from local device
  352. 1. Load 2nd stage system from network
  353. 2. Configure network interfaces (IPv4 only)
  354. 3. Load kernel modules from this disk
  355. 4. Load kernel modules from another disk
  356. 5. Activate already formatted swap device
  357. 6. Execute a shell (for experts!)
  358. 7. Validate a CD/DVD against its embedded checksum
  359. EOF
  360. echo -n "What do you want to do [0-7] (default=0)? "
  361. read text
  362. [ -z "${text}" ] && text=0
  363. input=${text//[^0-9]/}
  364. case "${input}" in
  365. 0)
  366. load_ramdisk_file 0
  367. ;;
  368. 1)
  369. httpload
  370. ;;
  371. 2)
  372. config_net
  373. ;;
  374. 3)
  375. load_modules "${mod_dir}"
  376. ;;
  377. 4)
  378. mkdir "/mnt_floppy" || echo "Can't create /mnt_floppy"
  379. trymount "/dev/floppy/0" "/mnt_floppy" && load_modules "/mnt_floppy"
  380. umount "/mnt_floppy" || echo "Can't umount /mnt_floppy"
  381. rmdir "/mnt_floppy" || echo "Can't remove /mnt_floppy"
  382. ;;
  383. 5)
  384. activate_swap
  385. ;;
  386. 6)
  387. exec_sh
  388. ;;
  389. 7)
  390. checkisomd5
  391. ;;
  392. *)
  393. echo "No such option present!"
  394. esac
  395. done
  396. exec /linuxrc
  397. echo "Can't start /linuxrc!! Life sucks.\n\n"