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.

495 lines
11 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. export 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. while : ; do
  156. echo -en "\nDevice file to use (q to return) : ";
  157. read device;
  158. [ "${device}" == "q" ] && return -1;
  159. if [ ! -e "${device}" ] ; then
  160. echo -e "\nNot a valid device!"
  161. else
  162. devicefile=${device}
  163. return 0;
  164. fi
  165. done
  166. } # }}}
  167. getcdromdevice () { # {{{
  168. cdroms="${1}"
  169. floppies="${2}"
  170. autoboot="${3}"
  171. devicelists="/dev/cdroms/* /dev/floppy/*"
  172. [ "${cdroms}" == "0" -a "${floppies}" == "0" ] && return -1
  173. devnr=0
  174. for dev in ${devicelists} ; do
  175. [ -e "${dev}" ] || continue
  176. [[ ${dev} = /dev/cdroms* ]] && [ "${cdroms}" == "0" ] && continue
  177. [[ ${dev} = /dev/floppy* ]] && [ "${floppies}" == "0" ] && continue
  178. eval "device_${devnr}='${dev}'"
  179. devnr=$((${devnr}+1))
  180. done
  181. [ ${devnr} -eq 0 ] && return -1
  182. x=0
  183. floppy=1
  184. cdrom=1
  185. while [ ${x} -lt ${devnr} ] ; do
  186. eval "device=\${device_${x}}"
  187. if [[ ${device} = /dev/cdrom* ]] ; then
  188. echo " ${x}. CD-ROM #${cdrom} (IDE/ATAPI or SCSI)"
  189. cdrom=$((${cdrom}+1))
  190. fi
  191. if [[ ${device} = /dev/flopp* ]] ; then
  192. echo " ${x}. FDD (Floppy Disk Drive) #${floppy}"
  193. floppy=$((${floppy}+1))
  194. fi
  195. x=$((${x}+1))
  196. done
  197. echo -en "\nEnter number or device file name (default=0): "
  198. if [ ${autoboot} -eq 1 ] ; then
  199. echo "0"
  200. text=0
  201. else
  202. read text
  203. fi
  204. [ -z "${text}" ] && text=0
  205. while : ; do
  206. if [ -e "${text}" ] ; then
  207. devicefile="${text}"
  208. return 0
  209. fi
  210. eval "text=\"\${device_${text}}\""
  211. if [ -n "${text}" ] ; then
  212. devicefile="${text}"
  213. return 0
  214. fi
  215. echo -n "No such device found. Try again (enter=back): "
  216. read text
  217. [ -z "${text}" ] && return -1
  218. done
  219. return 1;
  220. } # }}}
  221. load_ramdisk_file() { # {{{
  222. devicetype=${1}
  223. autoboot=${2}
  224. echo -en "Select a device for loading the 2nd stage system from: \n\n"
  225. if [ "${devicetype}" == "cdroms" ] ; then
  226. getcdromdevice 1 1 ${autoboot} || return
  227. else
  228. getdevice || return
  229. fi
  230. cat << EOF
  231. Select a stage 2 image file:
  232. 1. ${STAGE_2_BIG_IMAGE}
  233. 2. ${STAGE_2_SMALL_IMAGE}
  234. EOF
  235. echo -n "Enter number or image file name (default=1): "
  236. if [ ${autoboot} -eq 1 ] ; then
  237. echo "1"
  238. text=1
  239. else
  240. read text
  241. fi
  242. if [ -z "${text}" ] ; then
  243. filename="${STAGE_2_BIG_IMAGE}"
  244. elif [ "${text}" == "1" ] ; then
  245. filename="${STAGE_2_BIG_IMAGE}"
  246. elif [ "${text}" == "2" ] ; then
  247. filename="${STAGE_2_SMALL_IMAGE}"
  248. else
  249. filename="${text}"
  250. fi
  251. exit_linuxrc=1
  252. echo "Using ${devicefile}:${filename}."
  253. if ! mkdir -p /mnt_source ; then
  254. echo "Can't create /mnt_source"
  255. exit_linuxrc=0
  256. fi
  257. if ! mount ${devicefile} "/mnt_source" ; then
  258. echo "Can't mount /mnt_source"
  259. exit_linuxrc=0
  260. fi
  261. if ! mkdir -p /mnt_root ; then
  262. echo "Can't create /mnt_root"
  263. exit_linuxrc=0
  264. fi
  265. if ! mount -t tmpfs -o ${TMPFS_OPTIONS} none /mnt_root ; then
  266. echo "Can't mount tmpfs on /mnt_root"
  267. exit_linuxrc=0
  268. fi
  269. echo "Extracting 2nd stage filesystem to ram ..."
  270. if ! tar ${STAGE_2_COMPRESS_ARG} -C /mnt_root -xf /mnt_source/${filename} ; then
  271. echo "Can't extract /mnt/source/${filename}"
  272. exit_linuxrc=0
  273. return 1
  274. fi
  275. if ! umount "/mnt_source" ; then
  276. echo "Can't umount /mnt_source"
  277. exit_linuxrc=0
  278. fi
  279. if ! rmdir "/mnt_source" ; then
  280. echo "Can't remove /mnt_source"
  281. exit_linuxrc=0
  282. fi
  283. export ROCK_INSTALL_SOURCE_DEV=${devicefile}
  284. export ROCK_INSTALL_SOURCE_FILE=${filename}
  285. doboot
  286. } # }}}
  287. activate_swap() { # {{{
  288. echo
  289. echo -n "Enter file name of swap device: "
  290. read text
  291. if [ -n "${text}" ] ; then
  292. swapon ${text}
  293. fi
  294. } # }}}
  295. config_net() { # {{{
  296. ip addr
  297. echo
  298. ip route
  299. echo
  300. echo -n "Enter interface name (eth0): "
  301. read dv
  302. [ -z "${dv}" ] && dv="eth0"
  303. echo -n "Enter ip (192.168.0.254/24): "
  304. read ip
  305. [ -z "${ip}" ] && ip="192.168.0.254/24"
  306. ip addr add ${ip} dev ${dv}
  307. ip link set ${dv} up
  308. echo -n "Enter default gateway (none): "
  309. read gw
  310. [ -n "${gw}" ] && ip route add default via ${gw}
  311. ip addr
  312. echo
  313. ip route
  314. echo
  315. } # }}}
  316. autoload_modules () { # {{{
  317. while read cmd mod rest ; do
  318. [ -n "${rest}" ] && continue
  319. [ -z "${cmd}" ] && continue
  320. if [ "${cmd}" == "modprobe" -o "${cmd}" == "insmod" ] ; then
  321. echo "${cmd} ${mod}"
  322. ${cmd} ${mod} 2>&1 >/dev/null
  323. fi
  324. done < <( /bin/gawk -f /bin/hwscan )
  325. } # }}}
  326. exec_sh() { # {{{
  327. echo "Quit the shell to return to the stage 1 loader!"
  328. /bin/sh
  329. } # }}}
  330. checkisomd5() { # {{{
  331. echo "Select a device for checking: "
  332. getcdromdevice 1 0 0 || return
  333. echo "Running check..."
  334. /bin/checkisomd5 ${devicefile}
  335. echo "done"
  336. echo "Press Return key to continue."
  337. read
  338. } # }}}
  339. input=1
  340. exit_linuxrc=0
  341. [ -z "${autoboot}" ] && autoboot=0
  342. mount -t ramfs none /dev || echo "Can't mount a ramfs on /dev"
  343. mount -t sysfs none /sys || echo "Can't mount sysfs on /sys"
  344. mount -t proc none /proc || echo "Can't mount /proc"
  345. mount -t tmpfs -o ${TMPFS_OPTIONS} none /tmp || echo "Can't mount /tmpfs"
  346. udevstart
  347. cd /dev
  348. rm -rf fd
  349. ln -s /proc/self/fd
  350. cd -
  351. mod_load_info
  352. autoload_modules
  353. # some devices (scsi...) need time to settle...
  354. echo "Waiting for devices to settle..."
  355. sleep 5
  356. udevstart
  357. if [ ${autoboot} -eq 1 ] ; then
  358. load_ramdisk_file cdroms 1
  359. fi
  360. autoboot=0
  361. cat << EOF
  362. ============================================
  363. === ROCK Linux 1st stage boot system ===
  364. ============================================
  365. The ROCK Linux install / rescue system boots up in two stages. You
  366. are now in the first of this two stages and if everything goes right
  367. you will not spend much time here. Just load your SCSI and networking
  368. drivers (if needed) and configure the installation source so the
  369. 2nd stage boot system can be loaded and you can start the installation.
  370. EOF
  371. while [ ${exit_linuxrc} -eq 0 ] ; do
  372. cat <<EOF
  373. 0. Load 2nd stage system from cdrom or floppy drive
  374. 1. Load 2nd stage system from any device
  375. 2. Load 2nd stage system from network
  376. 3. Configure network interfaces (IPv4 only)
  377. 4. Load kernel modules from this disk
  378. 5. Load kernel modules from another disk
  379. 6. Activate already formatted swap device
  380. 7. Execute a shell (for experts!)
  381. 8. Validate a CD/DVD against its embedded checksum
  382. EOF
  383. echo -n "What do you want to do [0-7] (default=0)? "
  384. read text
  385. [ -z "${text}" ] && text=0
  386. input=${text//[^0-9]/}
  387. case "${input}" in
  388. 0)
  389. load_ramdisk_file cdroms 0
  390. ;;
  391. 1)
  392. load_ramdisk_file any 0
  393. ;;
  394. 2)
  395. httpload
  396. ;;
  397. 3)
  398. config_net
  399. ;;
  400. 4)
  401. load_modules "${mod_dir}"
  402. ;;
  403. 5)
  404. mkdir "/mnt_floppy" || echo "Can't create /mnt_floppy"
  405. trymount "/dev/floppy/0" "/mnt_floppy" && load_modules "/mnt_floppy"
  406. umount "/mnt_floppy" || echo "Can't umount /mnt_floppy"
  407. rmdir "/mnt_floppy" || echo "Can't remove /mnt_floppy"
  408. ;;
  409. 6)
  410. activate_swap
  411. ;;
  412. 7)
  413. exec_sh
  414. ;;
  415. 8)
  416. checkisomd5
  417. ;;
  418. *)
  419. echo "No such option present!"
  420. esac
  421. done
  422. exec /linuxrc
  423. echo "Can't start /linuxrc!! Life sucks.\n\n"