spacepaste

  1.  
  2. # cat init
  3. #!/bin/busybox sh
  4. export PATH=/sbin:/bin
  5. DEBUG=0
  6. rescue_shell() {
  7. echo "$@"
  8. echo "Something went wrong. Dropping you to a shell."
  9. busybox --install -s
  10. exec /bin/sh
  11. }
  12. # allow the use of UUIDs or filesystem lables
  13. uuidlabel_root() {
  14. for cmd in $(cat /proc/cmdline) ; do
  15. case ${cmd} in
  16. root=*)
  17. type=$(echo $cmd | cut -d= -f2)
  18. echo "Mounting rootfs"
  19. if [ ${type} == "LABEL" ] || [ ${type} == "UUID" ]; then
  20. uuid=$(echo $cmd | cut -d= -f3)
  21. mount -o ro $(findfs "${type}"="$uuid") /mnt/root
  22. else
  23. mount -o ro $(echo ${cmd} | cut -d= -f2) /mnt/root
  24. fi
  25. ;;
  26. esac
  27. done
  28. }
  29. check_filesystem() {
  30. # most of code coming from /etc/init.d/fsck
  31. local fsck_opts="" check_extra="" RC_UNAME=$(uname -s)
  32. local fs_type="$(grep $1 /mnt/root/etc/fstab | awk '{print 3}')" fsck_prefix=""
  33. # FIXME : get_bootparam forcefsck
  34. if [ -e /forcefsck ]; then
  35. fsck_opts="${fsck_opts} -f"
  36. check_extra="(check forced)"
  37. fi
  38. echo "Checking local filesystem $check_extra : $1"
  39. if [ "$RC_UNAME" = Linux ]; then
  40. case "${fs_type}" in
  41. reiserfs)
  42. fsck_opts="${fsck_opts} --check"
  43. fsck_prefix="echo Yes | "
  44. ;;
  45. ext4)
  46. fsck_opts="-p ${fsck_opts} -C0 -T"
  47. ;;
  48. *)
  49. rescue_shell "unknown fs type ${fs_type}"
  50. ;;
  51. esac
  52. fi
  53. trap : INT QUIT
  54. # using our own fsck, not the builtin one from busybox
  55. eval "${fsck_prefix}fsck.${fs_type} ${fsck_opts} $1"
  56. ret_val=$?
  57. case ${ret_val} in
  58. 0)
  59. return 0
  60. ;;
  61. 1)
  62. echo "Filesystem repaired";
  63. return 0
  64. ;;
  65. 2|3)
  66. if [ "${RC_UNAME}" = Linux ]; then
  67. echo "Filesystem repaired, but reboot needed"
  68. reboot -f
  69. else
  70. rescue_shell "Filesystem still have errors; manual fsck required"
  71. fi
  72. ;;
  73. 4)
  74. if [ "${RC_UNAME}" = Linux ]; then
  75. rescue_shell "Fileystem errors left uncorrected, aborting"
  76. else
  77. echo "Filesystem repaired, but reboot needed"
  78. reboot
  79. fi
  80. ;;
  81. 8)
  82. echo "Operational error"
  83. return 0
  84. ;;
  85. 16)
  86. echo "Use or Syntax Error"
  87. return 16
  88. ;;
  89. 32)
  90. echo "fsck interrupted"
  91. ;;
  92. 127)
  93. echo "Shared Library Error"
  94. sleep 20
  95. return 0
  96. ;;
  97. *)
  98. echo ${ret_val}
  99. echo "Some random fsck error - continuing anyway"
  100. sleep 20
  101. return 0
  102. ;;
  103. esac
  104. # rescue_shell can't find tty so its broken
  105. rescue_shell
  106. }
  107. create_labels() {
  108. mkdir -p /dev/disk/by-label
  109. while read partition; do
  110. DEV="$(echo ${partition} | awk '{print $4}')"
  111. DEV_PATH="/dev/${DEV}"
  112. if [ -z "${DEV}" -o ! -e "${DEV_PATH}" ]; then
  113. continue
  114. fi
  115. LABEL=$(busybox blkid ${DEV_PATH} | tr ' ' '\n' | grep "LABEL=" | cut -d= -f2 | sed 's/"//g')
  116. if [ ! -z "${LABEL}" ]; then
  117. ln -s ${DEV_PATH} /dev/disk/by-label/${LABEL}
  118. fi
  119. done < "/proc/partitions"
  120. }
  121. # start for real here
  122. # temporarily mount proc and sys
  123. mount -t proc none /proc
  124. mount -t sysfs none /sys
  125. mount -t devtmpfs none /dev
  126. CMDLINE="$(cat /proc/cmdline)"
  127. for param in ${CMDLINE}; do
  128. case "${param}" in
  129. rescue)
  130. echo "Rescue boot mode: invoking ash."
  131. rescue_shell "Rescue mode, dropping to shell"
  132. DEBUG=1
  133. ;;
  134. debug)
  135. DEBUG=1
  136. ;;
  137. esac
  138. done
  139. if [ ${DEBUG} -ne 0 ]; then
  140. # disable kernel messages from popping onto the screen
  141. echo 0 > /proc/sys/kernel/printk
  142. # clear the screen
  143. clear
  144. fi
  145. # assemble the raid set(s) - they got renumbered from md0, md1 and md2
  146. mdadm --assemble --scan
  147. #scan for raid inner partitions
  148. mdadm --detail --scan
  149. create_labels
  150. # mounting rootfs on /mnt/root
  151. uuidlabel_root || rescue_shell "Error with uuidlabel_root"
  152. # space separated list of mountpoints that ...
  153. mountpoints="/usr /var"
  154. # ... we want to find in /etc/fstab ...
  155. ln -s /mnt/root/etc/fstab /etc/fstab
  156. # ... to check filesystems and mount our devices.
  157. for m in ${mountpoints}; do
  158. check_filesystem ${m}
  159. echo "Mounting ${m}"
  160. # mount the device and ...
  161. mount ${m} || rescue_shell "Error while mounting ${m}"
  162. # ... move the tree to its final location
  163. mount --move ${m} "/mnt/root"${m} || rescue_shell "Error while moving $m"
  164. done
  165. echo "All done. Switching to real root."
  166. if [ ${DEBUG} -ne 0 ]; then
  167. echo 1 > /proc/sys/kernel/printk
  168. fi
  169. rm -rf /dev/disk/by-label
  170. # clean up. The init process will remount proc sys and dev later
  171. umount /proc
  172. umount /sys
  173. umount /dev
  174. # switch to the real root and execute init
  175. exec switch_root /mnt/root /sbin/init
  176. # cat initramfs_list
  177. # directory structure
  178. dir /proc 755 0 0
  179. dir /usr 755 0 0
  180. dir /bin 755 0 0
  181. dir /sys 755 0 0
  182. dir /var 755 0 0
  183. dir /lib64 755 0 0
  184. dir /sbin 755 0 0
  185. dir /mnt 755 0 0
  186. dir /mnt/root 755 0 0
  187. dir /etc 755 0 0
  188. dir /root 700 0 0
  189. dir /dev 755 0 0
  190. # busybox
  191. file /bin/busybox /usr/src/initramfs/bin/busybox 755 0 0
  192. # fsck bins
  193. file /sbin/fsck.reiserfs /sbin/fsck.reiserfs 755 0 0
  194. file /sbin/fsck.ext4 /sbin/fsck.ext4 755 0 0
  195. # mdadm
  196. file /sbin/mdadm /usr/src/initramfs/bin/mdadm 755 0 0
  197. file /etc/mdadm.conf /etc/mdadm.conf 755 0 0
  198. slink /lib /lib64 777 0 0
  199. file /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2 755 0 0
  200. file /lib64/libuuid.so.1 /lib64/libuuid.so.1 755 0 0
  201. file /lib64/libc.so.6 /lib64/libc.so.6 755 0 0
  202. file /lib/libext2fs.so.2 /lib/libext2fs.so.2 755 0 0
  203. file /lib/libcom_err.so.2 /lib/libcom_err.so.2 755 0 0
  204. file /lib/libpthread.so.0 /lib/libpthread.so.0 755 0 0
  205. file /lib/libblkid.so.1 /lib/libblkid.so.1 755 0 0
  206. file /lib/libe2p.so.2 /lib/libe2p.so.2 755 0 0
  207. # our init script
  208. file /init /usr/src/initramfs/init 755 0 0
  209.