spacepaste

  1.  
  2. #!/bin/busybox sh
  3. rescue_shell() {
  4. echo "$@"
  5. echo "Something went wrong. Dropping you to a shell."
  6. # have time to read the message
  7. /bin/sleep 20
  8. /bin/busybox --install -s
  9. exec /bin/sh
  10. }
  11. # allow the use of UUIDs or filesystem lables
  12. uuidlabel_root() {
  13. for cmd in $(/bin/cat /proc/cmdline) ; do
  14. case $cmd in
  15. root=*)
  16. type=$(echo $cmd | /bin/cut -d= -f2)
  17. echo $type
  18. echo "Mounting rootfs"
  19. if [ $type == "LABEL" ] || [ $type == "UUID" ] ; then
  20. uuid=$(echo $cmd | /bin/cut -d= -f3)
  21. /bin/mount -o ro $(/bin/findfs "$type"="$uuid") /mnt/root
  22. else
  23. /bin/mount -o ro $(echo $cmd | /bin/cut -d= -f2) /mnt/root
  24. fi
  25. #echo "Root contains"
  26. #ls -l /mnt/root
  27. #/bin/sleep 20
  28. ;;
  29. esac
  30. done
  31. }
  32. check_filesystem() {
  33. # most of code coming from /etc/init.d/fsck
  34. local fsck_opts= check_extra= RC_UNAME=$(uname -s)
  35. # FIXME : get_bootparam forcefsck
  36. if [ -e /forcefsck ]; then
  37. fsck_opts="$fsck_opts -f"
  38. check_extra="(check forced)"
  39. fi
  40. echo "Checking local filesystem $check_extra : $1"
  41. #/bin/sleep 20
  42. if [ "$RC_UNAME" = Linux ]; then
  43. fsck_opts="$fsck_opts -C0 -T"
  44. fi
  45. trap : INT QUIT
  46. # using our own fsck, not the builtin one from busybox
  47. /sbin/fsck -p $fsck_opts $1
  48. ret_val=$?
  49. case $ret_val in
  50. 0) return 0;;
  51. 1) echo "Filesystem repaired"; return 0;;
  52. 2|3) if [ "$RC_UNAME" = Linux ]; then
  53. echo "Filesystem repaired, but reboot needed"
  54. reboot -f
  55. else
  56. rescue_shell "Filesystem still have errors; manual fsck required"
  57. fi;;
  58. 4) if [ "$RC_UNAME" = Linux ]; then
  59. rescue_shell "Fileystem errors left uncorrected, aborting"
  60. else
  61. echo "Filesystem repaired, but reboot needed"
  62. reboot -f
  63. fi;;
  64. 8) echo "Operational error"; return 0;;
  65. 16) echo "Use or Syntax Error"; return 16;;
  66. 32) echo "fsck interrupted";;
  67. 127) echo "Shared Library Error"; sleep 20; return 0;;
  68. *) echo $ret_val; echo "Some random fsck error - continuing anyway"; sleep 20; return 0;;
  69. esac
  70. # rescue_shell can't find tty so its broken
  71. rescue_shell
  72. }
  73. # start for real here
  74. # temporarily mount proc and sys
  75. PATH=/sbin:/bin
  76. export PATH
  77. #echo "/bin"
  78. #/bin/ls -l /bin
  79. #echo "/sbin"
  80. #/bin/ls -l /sbin
  81. #/bin/sleep 20
  82. #echo "Mount proc"
  83. /bin/mount -t proc proc /proc
  84. #/bin/busybox sleep 20
  85. #echo "Mount sysfs"
  86. /bin/mount -t sysfs sysfs /sys
  87. #/bin/ls /dev
  88. #/bin/sleep 20
  89. # Don't be silly - /dev is static and its on /
  90. # echo "Mount /dev"
  91. # /bin/busybox mount -t devtmpfs none /dev
  92. # /bin/busybox sleep 20
  93. # /bin/busybox ls /dev
  94. # /bin/busybox sleep 20
  95. # disable kernel messages from popping onto the screen
  96. ###echo 0 > /proc/sys/kernel/printk
  97. # clear the screen
  98. ###clear
  99. # assemble the raid set(s) - they got renumbered from md1, md5 and md6
  100. #/dev/sda1: UUID="9392926d-6408-6e7a-8663-82834138a597" TYPE="linux_raid_member" PARTUUID="0553caf4-01"
  101. #/dev/sda2: UUID="b6633d8e-41ef-4485-9bbe-c4c2d69f4e8c" TYPE="swap" PARTUUID="0553caf4-02"
  102. #/dev/sda5: UUID="5e3cadd4-cfd2-665d-9690-1ac76d8f5a5d" TYPE="linux_raid_member" PARTUUID="0553caf4-05"
  103. #/dev/sda6: UUID="9657e667-5b60-f6a3-0391-65e6dcf662fa" TYPE="linux_raid_member" PARTUUID="0553caf4-06"
  104. # not needed on SSD but we may want to maintain it
  105. # spinney /boot
  106. /sbin/mdadm --assemble /dev/md125 --uuid=9392926d-6408-6e7a-8663-82834138a597
  107. # /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
  108. # don't care if /boot fails to assemble
  109. # not needed on SSD, so don't fail for not assembling spinney root
  110. # / (root) I wimped out of root on lvm for this box
  111. /sbin/mdadm --assemble /dev/md126 --uuid=5e3cadd4-cfd2-665d-9690-1ac76d8f5a5d
  112. # /dev/sda5 /dev/sdb5 /dev/sdc5 /dev/sdd5 || rescue_shell "Root Assembly Failed"
  113. # LVM for everything else
  114. # /home and everything portge related
  115. /sbin/mdadm --assemble /dev/md127 --uuid=9657e667-5b60-f6a3-0391-65e6dcf662fa
  116. #/dev/sda6 /dev/sdb6 /dev/sdc6 /dev/sdd6 || rescue_shell "LVM Assembly Failed"
  117. # and if the LVM space won't assemble there is no /usr or /var so we are really in a mess
  118. # TODO could auto cope with degraded raid operation
  119. # start the vg volume group - /home and everything for portage - need not die here
  120. /sbin/vgchange -ay vg || rescue_shell "lvchange vg failed"
  121. ## WARNING Order is important!!! or the /dev/dm-x devices come up in the wrong places.
  122. # if this failed we have no, / /usr or /var
  123. # everything on the SDD
  124. /sbin/vgchange -ay static || rescue_shell "lvchange static failed"
  125. # get here with raid sets assembled and logical volumes available
  126. # mounting rootfs on /mnt/root
  127. uuidlabel_root || rescue_shell "Error with uuidlabel_root"
  128. # space separated list of mountpoints that ...
  129. mountpoints="/usr /var"
  130. # ... we want to find in /etc/fstab ...
  131. /bin/ln -s /mnt/root/etc/fstab /etc/fstab
  132. #/bin/ls -l /etc
  133. #/bin/ls -l /mnt/root
  134. #/bin/sleep 20
  135. # ... to check filesystems and mount our devices.
  136. for m in $mountpoints ; do
  137. #echo $m
  138. #/bin/sleep 20
  139. check_filesystem $m
  140. echo "Mounting $m"
  141. # mount the device and ...
  142. /bin/mount $m || rescue_shell "Error while mounting $m"
  143. # ... move the tree to its final location
  144. /bin/mount --move $m "/mnt/root"$m || rescue_shell "Error while moving $m"
  145. done
  146. echo "All done. Switching to real root."
  147. # clean up. The init process will remount proc sys and dev later
  148. /bin/umount /proc
  149. /bin/umount /sys
  150. # we don't have a /dev to umount
  151. # switch to the real root and execute init
  152. exec /sbin/switch_root /mnt/root /sbin/init
  153.