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.

505 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. mountopts=""
  225. echo -en "Select a device for loading the 2nd stage system from: \n\n"
  226. if [ "${devicetype}" == "cdroms" ] ; then
  227. getcdromdevice 1 1 ${autoboot} || return
  228. mountopts="-o ro"
  229. else
  230. getdevice || return
  231. fi
  232. cat << EOF
  233. Select a stage 2 image file:
  234. 1. ${STAGE_2_BIG_IMAGE}
  235. 2. ${STAGE_2_SMALL_IMAGE}
  236. EOF
  237. echo -n "Enter number or image file name (default=1): "
  238. if [ ${autoboot} -eq 1 ] ; then
  239. echo "1"
  240. text=1
  241. else
  242. read text
  243. fi
  244. if [ -z "${text}" ] ; then
  245. filename="${STAGE_2_BIG_IMAGE}"
  246. elif [ "${text}" == "1" ] ; then
  247. filename="${STAGE_2_BIG_IMAGE}"
  248. elif [ "${text}" == "2" ] ; then
  249. filename="${STAGE_2_SMALL_IMAGE}"
  250. else
  251. filename="${text}"
  252. fi
  253. exit_linuxrc=1
  254. echo "Using ${devicefile}:${filename}."
  255. if ! mkdir -p /mnt_source ; then
  256. echo "Can't create /mnt_source"
  257. exit_linuxrc=0
  258. fi
  259. if ! mount ${mountopts} ${devicefile} "/mnt_source" ; then
  260. echo "Can't mount /mnt_source"
  261. exit_linuxrc=0
  262. fi
  263. if ! mkdir -p /mnt_root ; then
  264. echo "Can't create /mnt_root"
  265. exit_linuxrc=0
  266. fi
  267. if ! mount -t tmpfs -o ${TMPFS_OPTIONS} tmpfs /mnt_root ; then
  268. echo "Can't mount tmpfs on /mnt_root"
  269. exit_linuxrc=0
  270. fi
  271. echo "Extracting 2nd stage filesystem to ram ..."
  272. if ! tar ${STAGE_2_COMPRESS_ARG} -C /mnt_root -xf /mnt_source/${filename} ; then
  273. echo "Can't extract /mnt/source/${filename}"
  274. exit_linuxrc=0
  275. return 1
  276. fi
  277. if ! umount "/mnt_source" ; then
  278. echo "Can't umount /mnt_source"
  279. exit_linuxrc=0
  280. fi
  281. if ! rmdir "/mnt_source" ; then
  282. echo "Can't remove /mnt_source"
  283. exit_linuxrc=0
  284. fi
  285. export ROCK_INSTALL_SOURCE_DEV=${devicefile}
  286. export ROCK_INSTALL_SOURCE_FILE=${filename}
  287. doboot
  288. } # }}}
  289. activate_swap() { # {{{
  290. echo
  291. echo -n "Enter file name of swap device: "
  292. read text
  293. if [ -n "${text}" ] ; then
  294. swapon ${text}
  295. fi
  296. } # }}}
  297. config_net() { # {{{
  298. ip addr
  299. echo
  300. ip route
  301. echo
  302. echo -n "Enter interface name (eth0): "
  303. read dv
  304. [ -z "${dv}" ] && dv="eth0"
  305. echo -n "Enter ip (192.168.0.254/24): "
  306. read ip
  307. [ -z "${ip}" ] && ip="192.168.0.254/24"
  308. ip addr add ${ip} dev ${dv}
  309. ip link set ${dv} up
  310. echo -n "Enter default gateway (none): "
  311. read gw
  312. [ -n "${gw}" ] && ip route add default via ${gw}
  313. ip addr
  314. echo
  315. ip route
  316. echo
  317. } # }}}
  318. exec_sh() { # {{{
  319. echo "Quit the shell to return to the stage 1 loader!"
  320. /bin/sh
  321. } # }}}
  322. checkisomd5() { # {{{
  323. echo "Select a device for checking: "
  324. getcdromdevice 1 0 0 || return
  325. echo "Running check..."
  326. /bin/checkisomd5 ${devicefile}
  327. code=${?}
  328. if [ ${code} -eq 1 ] ; then
  329. echo "MD5Sum is correct."
  330. elif [ ${code} -eq 0 ] ; then
  331. echo "MD5Sum is NOT correct! Please contact the authors!"
  332. fi
  333. echo "Press Return key to continue."
  334. read
  335. } # }}}
  336. emit_udev_events() { # {{{
  337. /sbin/udevtrigger
  338. /sbin/udevsettle
  339. } # }}}
  340. input=1
  341. exit_linuxrc=0
  342. [ -z "${autoboot}" ] && autoboot=0
  343. # mount / / -o remount,rw || echo "Can't remount / read-/writeable"
  344. # mount / / -o remount,rw || echo "Can't remount / read-/writeable (for mount log)"
  345. mount -t tmpfs tmpfs /tmp -o ${TMPFS_OPTIONS} || echo "Can't mount a tmpfs on /tmp"
  346. mount -t proc proc /proc || echo "Can't mount proc on /proc!"
  347. mount -t sysfs sysfs /sys || echo "Can't mount sysfs on /sys!"
  348. mount -t tmpfs tmpfs /dev || echo "Can't mount a tmpfs on /dev!"
  349. /sbin/depmod -ae
  350. cp -r /lib/udev/devices/* /dev
  351. echo "" > /proc/sys/kernel/hotplug
  352. /sbin/udevd --daemon
  353. # create nodes for devices already in kernel
  354. emit_udev_events
  355. mod_load_info
  356. # some devices (scsi...) need time to settle...
  357. echo "Waiting for devices to settle..."
  358. sleep 5
  359. ip addr add 127.0.0.1 dev lo
  360. ip link set lo up
  361. if [ ${autoboot} -eq 1 ] ; then
  362. load_ramdisk_file cdroms 1
  363. fi
  364. autoboot=0
  365. cat << EOF
  366. ============================================
  367. === ROCK Linux 1st stage boot system ===
  368. ============================================
  369. The ROCK Linux install / rescue system boots up in two stages. You
  370. are now in the first of this two stages and if everything goes right
  371. you will not spend much time here. Just load your SCSI and networking
  372. drivers (if needed) and configure the installation source so the
  373. 2nd stage boot system can be loaded and you can start the installation.
  374. EOF
  375. while [ ${exit_linuxrc} -eq 0 ] ; do
  376. cat <<EOF
  377. 0. Load 2nd stage system from cdrom or floppy drive
  378. 1. Load 2nd stage system from any device
  379. 2. Load 2nd stage system from network
  380. 3. Configure network interfaces (IPv4 only)
  381. 4. Load kernel modules from this disk
  382. 5. Load kernel modules from another disk
  383. 6. Activate already formatted swap device
  384. 7. Execute a shell (for experts!)
  385. 8. Validate a CD/DVD against its embedded checksum
  386. EOF
  387. echo -n "What do you want to do [0-8] (default=0)? "
  388. read text
  389. [ -z "${text}" ] && text=0
  390. input=${text//[^0-9]/}
  391. case "${input}" in
  392. 0)
  393. load_ramdisk_file cdroms 0
  394. ;;
  395. 1)
  396. load_ramdisk_file any 0
  397. ;;
  398. 2)
  399. httpload
  400. ;;
  401. 3)
  402. config_net
  403. ;;
  404. 4)
  405. load_modules "${mod_dir}"
  406. ;;
  407. 5)
  408. mkdir "/mnt_floppy" || echo "Can't create /mnt_floppy"
  409. trymount "/dev/floppy/0" "/mnt_floppy" && load_modules "/mnt_floppy"
  410. umount "/mnt_floppy" || echo "Can't umount /mnt_floppy"
  411. rmdir "/mnt_floppy" || echo "Can't remove /mnt_floppy"
  412. ;;
  413. 6)
  414. activate_swap
  415. ;;
  416. 7)
  417. exec_sh
  418. ;;
  419. 8)
  420. checkisomd5
  421. ;;
  422. *)
  423. echo "No such option present!"
  424. esac
  425. done
  426. exec /sbin/init
  427. echo "Can't start /sbin/init!! Life sucks.\n\n"