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.

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