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.

501 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/sbin/init ] ; then
  33. echo "Can't find /mnt_root/sbin/init!"
  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} tmpfs /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. exec_sh() { # {{{
  317. echo "Quit the shell to return to the stage 1 loader!"
  318. /bin/sh
  319. } # }}}
  320. checkisomd5() { # {{{
  321. echo "Select a device for checking: "
  322. getcdromdevice 1 0 0 || return
  323. echo "Running check..."
  324. /bin/checkisomd5 ${devicefile}
  325. code=${?}
  326. if [ ${code} -eq 1 ] ; then
  327. echo "MD5Sum is correct."
  328. elif [ ${code} -eq 0 ] ; then
  329. echo "MD5Sum is NOT correct! Please contact the authors!"
  330. fi
  331. echo "Press Return key to continue."
  332. read
  333. } # }}}
  334. emit_udev_events() { # {{{
  335. /sbin/udevtrigger
  336. /sbin/udevsettle
  337. } # }}}
  338. input=1
  339. exit_linuxrc=0
  340. [ -z "${autoboot}" ] && autoboot=0
  341. # mount / / -o remount,rw || echo "Can't remount / read-/writeable"
  342. # mount / / -o remount,rw || echo "Can't remount / read-/writeable (for mount log)"
  343. mount -t tmpfs tmpfs /tmp -o ${TMPFS_OPTIONS} || echo "Can't mount a tmpfs on /tmp"
  344. mount -t proc proc /proc || echo "Can't mount proc on /proc!"
  345. mount -t sysfs sysfs /sys || echo "Can't mount sysfs on /sys!"
  346. mount -t tmpfs tmpfs /dev || echo "Can't mount a tmpfs on /dev!"
  347. cp -r /lib/udev/devices/* /dev
  348. echo "" > /proc/sys/kernel/hotplug
  349. /sbin/udevd --daemon
  350. # create nodes for devices already in kernel
  351. emit_udev_events
  352. mod_load_info
  353. # some devices (scsi...) need time to settle...
  354. echo "Waiting for devices to settle..."
  355. sleep 5
  356. ip addr add 127.0.0.1 dev lo
  357. ip link set lo up
  358. if [ ${autoboot} -eq 1 ] ; then
  359. load_ramdisk_file cdroms 1
  360. fi
  361. autoboot=0
  362. cat << EOF
  363. ============================================
  364. === ROCK Linux 1st stage boot system ===
  365. ============================================
  366. The ROCK Linux install / rescue system boots up in two stages. You
  367. are now in the first of this two stages and if everything goes right
  368. you will not spend much time here. Just load your SCSI and networking
  369. drivers (if needed) and configure the installation source so the
  370. 2nd stage boot system can be loaded and you can start the installation.
  371. EOF
  372. while [ ${exit_linuxrc} -eq 0 ] ; do
  373. cat <<EOF
  374. 0. Load 2nd stage system from cdrom or floppy drive
  375. 1. Load 2nd stage system from any device
  376. 2. Load 2nd stage system from network
  377. 3. Configure network interfaces (IPv4 only)
  378. 4. Load kernel modules from this disk
  379. 5. Load kernel modules from another disk
  380. 6. Activate already formatted swap device
  381. 7. Execute a shell (for experts!)
  382. 8. Validate a CD/DVD against its embedded checksum
  383. EOF
  384. echo -n "What do you want to do [0-8] (default=0)? "
  385. read text
  386. [ -z "${text}" ] && text=0
  387. input=${text//[^0-9]/}
  388. case "${input}" in
  389. 0)
  390. load_ramdisk_file cdroms 0
  391. ;;
  392. 1)
  393. load_ramdisk_file any 0
  394. ;;
  395. 2)
  396. httpload
  397. ;;
  398. 3)
  399. config_net
  400. ;;
  401. 4)
  402. load_modules "${mod_dir}"
  403. ;;
  404. 5)
  405. mkdir "/mnt_floppy" || echo "Can't create /mnt_floppy"
  406. trymount "/dev/floppy/0" "/mnt_floppy" && load_modules "/mnt_floppy"
  407. umount "/mnt_floppy" || echo "Can't umount /mnt_floppy"
  408. rmdir "/mnt_floppy" || echo "Can't remove /mnt_floppy"
  409. ;;
  410. 6)
  411. activate_swap
  412. ;;
  413. 7)
  414. exec_sh
  415. ;;
  416. 8)
  417. checkisomd5
  418. ;;
  419. *)
  420. echo "No such option present!"
  421. esac
  422. done
  423. exec /sbin/init
  424. echo "Can't start /sbin/init!! Life sucks.\n\n"