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.

498 lines
11 KiB

  1. #!/bin/bash
  2. STAGE_2_BIG_IMAGE="2nd_stage.img.z"
  3. #640kB, err, 64 MB should be enought for the tmpfs ;-)
  4. TMPFS_OPTIONS="size=67108864"
  5. mod_load_info () { # {{{
  6. read os host version rest < <( uname -a )
  7. if [ -z "${os}" ] ; then
  8. echo "Can't run \`uname -a\`"
  9. return
  10. elif [ "${os}" != "Linux" ] ; then
  11. echo "Your operating system is not supported ?!"
  12. return
  13. fi
  14. mod_loader="/sbin/insmod"
  15. mod_dir="/lib/modules/"
  16. # kernel module suffix for <= 2.4 is .o, .ko if above
  17. if [ ${version:2:1} -gt 4 ] ; then
  18. mod_suffix=".ko"
  19. mod_suffix_len=3
  20. else
  21. mod_suffix=".o"
  22. mod_suffix_len=2
  23. fi
  24. } # }}}
  25. doboot() { # {{{
  26. echo "doboot starting - trying to exec /sbin/init"
  27. exec /sbin/init
  28. } # }}}
  29. trymount() { # {{{
  30. source=${1}
  31. target=${2}
  32. mount -t iso9600 -o ro ${source} ${target} && return 0
  33. mount -t ext3 -o ro ${source} ${target} && return 0
  34. mount -t ext2 -o ro ${source} ${target} && return 0
  35. mount -t minix -o ro ${source} ${target} && return 0
  36. mount -t vfat -o ro ${source} ${target} && return 0
  37. return -1
  38. } # }}}
  39. httpload() { # {{{
  40. echo -n "Enter base URL (e.g. http://1.2.3.4/rock): "
  41. read baseurl
  42. [ -z "${baseurl}" ] && return
  43. cat <<EOF
  44. Select a stage 2 image file:
  45. 0. ${STAGE_2_BIG_IMAGE}
  46. 1. ${STAGE_2_SMALL_IMAGE}
  47. EOF
  48. echo -n "Enter number or image file name (default=0): "
  49. read filename
  50. if [ -z "${filename}" ] ; then
  51. filename="${STAGE_2_BIG_IMAGE}"
  52. elif [ "${filename}" == "0" ] ; then
  53. filename=${STAGE_2_BIG_IMAGE}
  54. elif [ "${filename}" == "1" ] ; then
  55. filename="${STAGE_2_SMALL_IMAGE}"
  56. fi
  57. url="${baseurl%/}/${filename}"
  58. echo "[ ${url} ]"
  59. export ROCK_INSTALL_SOURCE_URL=${baseurl}
  60. exit_linuxrc=1;
  61. if ! mkdir /mnt_root ; then
  62. echo "Can't create /mnt_root"
  63. exit_linuxrc=0
  64. fi
  65. if ! mount -t tmpfs -O ${TMPFS_OPTIONS} none /mnt_root ; then
  66. echo "Can't mount /mnt_root"
  67. exit_linuxrc=0
  68. fi
  69. wget -O - ${url} | tar ${STAGE_2_COMPRESS_ARG} -C /mnt_root -xf -
  70. echo "finished ... now booting 2nd stage"
  71. doboot
  72. } # }}}
  73. load_modules() { # {{{
  74. # this starts the module loading shell
  75. directory=${1}
  76. cat <<EOF
  77. module loading shell
  78. you can navigate through the filestem with 'cd'. for loading a module
  79. simply enter the shown name, to exit press enter on a blank line.
  80. EOF
  81. cd ${directory}
  82. while : ; do
  83. echo "Directories:"
  84. count=0
  85. while read inode ; do
  86. [ -d "${inode}" ] || continue
  87. echo -n " [ ${inode} ]"
  88. count=$((${count}+1))
  89. if [ ${count} -gt 3 ] ; then
  90. echo
  91. count=0
  92. fi
  93. done < <( ls ) | expand -t1,3,19,21,23,39,41,43,59,61,63,78
  94. echo
  95. echo "Modules:"
  96. count=0
  97. while read inode ; do
  98. [ -f "${inode}" ] || continue
  99. [ "${inode%${mod_suffix}}" == "${inode}" ] && continue
  100. echo -n " [ ${inode%${mod_suffix}} ]"
  101. count=$((${count}+1))
  102. if [ ${count} -gt 3 ] ; then
  103. echo
  104. count=0
  105. fi
  106. done < <( ls ) | expand -t1,3,19,21,23,39,41,43,59,61,63,78
  107. echo
  108. echo -n "[${PWD##*/} ] > "
  109. read cmd par
  110. if [ "${cmd}" == "cd" ] ; then
  111. cd ${par}
  112. elif [ -f "${cmd}${mod_suffix}" ] ; then
  113. insmod ${PWD%/}/${cmd}${mod_suffix} ${par}
  114. elif [ -z "${cmd}" ] ; then
  115. break
  116. else
  117. echo "No such module: ${cmd}"
  118. fi
  119. done
  120. return
  121. } # }}}
  122. getdevice () { # {{{
  123. while : ; do
  124. echo -en "\nDevice file to use (q to return) : ";
  125. read device;
  126. [ "${device}" == "q" ] && return -1;
  127. if [ ! -e "${device}" ] ; then
  128. echo -e "\nNot a valid device!"
  129. else
  130. devicefile=${device}
  131. return 0;
  132. fi
  133. done
  134. } # }}}
  135. getcdromdevice () { # {{{
  136. cdroms="${1}"
  137. floppies="${2}"
  138. autoboot="${3}"
  139. devicelists="/dev/cdroms/* /dev/hd[a-d] /dev/floppy/*"
  140. [ "${cdroms}" == "0" -a "${floppies}" == "0" ] && return -1
  141. devnr=0
  142. for dev in ${devicelists} ; do
  143. [ -e "${dev}" ] || continue
  144. [[ ${dev} = /dev/cdroms* ]] && [ "${cdroms}" == "0" ] && continue
  145. [[ ${dev} = /dev/floppy* ]] && [ "${floppies}" == "0" ] && continue
  146. eval "device_${devnr}='${dev}'"
  147. devnr=$((${devnr}+1))
  148. done
  149. [ ${devnr} -eq 0 ] && return -1
  150. x=0
  151. floppy=1
  152. cdrom=1
  153. while [ ${x} -lt ${devnr} ] ; do
  154. eval "device=\${device_${x}}"
  155. if [[ ${device} = /dev/cdrom* ]] ; then
  156. echo " ${x}. CD-ROM #${cdrom} (IDE/ATAPI or SCSI)"
  157. cdrom=$((${cdrom}+1))
  158. fi
  159. if [[ ${device} = /dev/floppy* ]] ; then
  160. echo " ${x}. FDD (Floppy Disk Drive) #${floppy}"
  161. floppy=$((${floppy}+1))
  162. fi
  163. if [[ ${device} = /dev/hd[a-d] ]] ; then
  164. echo " ${x}. IDE Drive #${x}"
  165. floppy=$((${floppy}+1))
  166. fi
  167. x=$((${x}+1))
  168. done
  169. echo -en "\nEnter number or device file name (default=0): "
  170. if [ ${autoboot} -eq 1 ] ; then
  171. echo "0"
  172. text=0
  173. else
  174. read text
  175. fi
  176. [ -z "${text}" ] && text=0
  177. while : ; do
  178. if [ -e "${text}" ] ; then
  179. devicefile="${text}"
  180. return 0
  181. fi
  182. eval "text=\"\${device_${text}}\""
  183. if [ -n "${text}" ] ; then
  184. devicefile="${text}"
  185. return 0
  186. fi
  187. echo -n "No such device found. Try again (enter=back): "
  188. read text
  189. [ -z "${text}" ] && return -1
  190. done
  191. return 1;
  192. } # }}}
  193. prepare_root () {
  194. local ret=0 F
  195. echo "preparing /dev"
  196. cd /dev || ret=1
  197. rm -rf fd
  198. ln -svf /proc/kcore core || ret=1
  199. ln -svf /proc/self/fd fd || ret=1
  200. ln -svf fd/0 stdin || ret=1
  201. ln -svf fd/1 stdout || ret=1
  202. ln -svf fd/2 stderr || ret=1
  203. cd / || ret=1
  204. ln -svf /mnt/cowfs_ro/* /mnt/cowfs_rw-new/
  205. rm -rf /mnt/cowfs_rw-new/{home,tmp}
  206. mkdir -p /mnt/cowfs_rw-new/{home,tmp}
  207. mkdir -p /mnt/cowfs_rw-new/home/{rocker,root}
  208. chmod 755 /mnt/cowfs_rw-new/home/rocker
  209. chmod 700 /mnt/cowfs_rw-new/home/root
  210. if ! chown 1000:100 /mnt/cowfs_rw-new/home/rocker ; then
  211. echo "could not chown /mnt/cowfs_rw-new/home/rocker to rocker:users"
  212. ret=1
  213. fi
  214. mount --move /mnt/cowfs_rw-new /mnt/cowfs_rw
  215. rm -rf /mnt/cowfs-rw-new
  216. return $ret
  217. }
  218. load_ramdisk_file() { # {{{
  219. local devicetype=${1} autoboot=${2}
  220. echo -en "Select a device for loading the 2nd stage system from: \n\n"
  221. if [ "${devicetype}" == "cdroms" ] ; then
  222. getcdromdevice 1 1 ${autoboot} || return
  223. else
  224. getdevice || return
  225. fi
  226. cat << EOF
  227. Select a stage 2 image file:
  228. 1. ${STAGE_2_BIG_IMAGE}
  229. EOF
  230. echo -n "Enter number or image file name (default=1): "
  231. if [ ${autoboot} -eq 1 ] ; then
  232. text=1
  233. else
  234. read text
  235. fi
  236. if [ -z "${text}" -o "${text}" = "1" ] ; then
  237. filename="${STAGE_2_BIG_IMAGE}"
  238. else
  239. filename="${text}"
  240. fi
  241. exit_linuxrc=1
  242. echo "Using ${devicefile}://${filename}."
  243. if ! mkdir -p /mnt/cdrom ; then
  244. echo "Can't create /mnt/cdrom"
  245. exit_linuxrc=0
  246. fi
  247. if ! mount ${devicefile} "/mnt/cdrom" -o ro ; then
  248. echo "Can't mount /mnt/cdrom"
  249. exit_linuxrc=0
  250. fi
  251. if ! losetup /dev/loop/0 "/mnt/cdrom/${filename}" ; then
  252. echo "Can't losetup /mnt/cdrom/${filename}"
  253. exit_linuxrc=0
  254. fi
  255. # mkdir -p /mnt/cowfs_r{o,w}
  256. if ! mount -t squashfs /dev/loop/0 /mnt/cowfs_ro -o ro ; then
  257. echo "Can't mount squashfs on /mnt/cowfs_ro"
  258. exit_linuxrc=0
  259. fi
  260. if ! mkdir -p /mnt/cowfs_rw-new ; then
  261. echo "Can't mkdir -p /mnt/cowfs_rw-new"
  262. exit_linuxrc=0
  263. fi
  264. if ! mount -t tmpfs -o ${TMPFS_OPTIONS} tmpfs /mnt/cowfs_rw-new ; then
  265. echo "Can't mount tmpfs on /mnt/cowfs_rw-new"
  266. exit_linuxrc=0
  267. fi
  268. export ROCK_INSTALL_SOURCE_DEV=${devicefile}
  269. export ROCK_INSTALL_SOURCE_FILE=${filename}
  270. if prepare_root ; then
  271. rm -rf /mnt/cowfs_rw-new
  272. doboot
  273. fi
  274. } # }}}
  275. activate_swap() { # {{{
  276. echo
  277. echo -n "Enter file name of swap device: "
  278. read text
  279. if [ -n "${text}" ] ; then
  280. swapon ${text}
  281. fi
  282. } # }}}
  283. config_net() { # {{{
  284. ip addr
  285. echo
  286. ip route
  287. echo
  288. echo -n "Enter interface name (eth0): "
  289. read dv
  290. [ -z "${dv}" ] && dv="eth0"
  291. echo -n "Enter ip (192.168.0.254/24): "
  292. read ip
  293. [ -z "${ip}" ] && ip="192.168.0.254/24"
  294. ip addr add ${ip} dev ${dv}
  295. ip link set ${dv} up
  296. echo -n "Enter default gateway (none): "
  297. read gw
  298. [ -n "${gw}" ] && ip route add default via ${gw}
  299. ip addr
  300. echo
  301. ip route
  302. echo
  303. } # }}}
  304. autoload_modules () { # {{{
  305. while read cmd mod rest ; do
  306. [ -n "${rest}" ] && continue
  307. [ -z "${cmd}" ] && continue
  308. if [ "${cmd}" == "modprobe" -o "${cmd}" == "insmod" ] ; then
  309. echo "${cmd} ${mod}"
  310. ${cmd} ${mod} 2>&1 >/dev/null
  311. fi
  312. done < <( /bin/gawk -f /sbin/hwscan )
  313. } # }}}
  314. exec_sh() { # {{{
  315. echo "Quit the shell to return to the stage 1 loader!"
  316. /bin/sh
  317. } # }}}
  318. checkisomd5() { # {{{
  319. echo "Select a device for checking: "
  320. getcdromdevice 1 0 0 || return
  321. echo "Running check..."
  322. /bin/checkisomd5 ${devicefile}
  323. echo "done"
  324. echo "Press Return key to continue."
  325. read
  326. } # }}}
  327. input=1
  328. exit_linuxrc=0
  329. [ -z "${autoboot}" ] && autoboot=0
  330. mount -t tmpfs tmpfs /dev || echo "Can't mount a tmpfs on /dev"
  331. mount -t sysfs sysfs /sys || echo "Can't mount sysfs on /sys"
  332. mount -t proc proc /proc || echo "Can't mount /proc"
  333. mount -t tmpfs -o ${TMPFS_OPTIONS} tmpfs /tmp || echo "Can't mount /tmpfs"
  334. cd /dev
  335. rm -rf fd
  336. ln -s /proc/self/fd
  337. cd -
  338. export PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"
  339. echo "" > /proc/sys/kernel/hotplug
  340. /sbin/udevd --daemon
  341. # create nodes for devices already in kernel
  342. while read uevent; do
  343. echo 1 > $uevent
  344. done < <( find /sys -name uevent )
  345. udevwait=0
  346. while [ -d /dev/.udev/queue -a $udevwait -lt 300 ] ; do
  347. sleep 1
  348. (( udevwait++ ))
  349. done
  350. mod_load_info
  351. autoload_modules
  352. # some devices (scsi...) need time to settle...
  353. echo "Waiting for devices to settle..."
  354. sleep 5
  355. ip addr add 127.0.0.1 dev lo
  356. ip link set lo up
  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"