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.

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