/packages/initramfs/sysutils/busybox-initramfs/scripts/init

http://github.com/OpenELEC/OpenELEC.tv · Shell · 263 lines · 187 code · 45 blank · 31 comment · 35 complexity · cdeb941771e5037225b0667e779401a5 MD5 · raw file

  1. #!/bin/sh
  2. ################################################################################
  3. # This file is part of OpenELEC - http://www.openelec.tv
  4. # Copyright (C) 2009-2011 Stephan Raue (stephan@openelec.tv)
  5. #      Copyright (C) 2010-2011 Roman Weber (roman@openelec.tv)
  6. #
  7. # This Program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2, or (at your option)
  10. # any later version.
  11. #
  12. # This Program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with OpenELEC.tv; see the file COPYING. If not, write to
  19. # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20. # http://www.gnu.org/copyleft/gpl.html
  21. ################################################################################
  22. UPDATE_DIR=/storage/.update
  23. IMAGE_SYSTEM="SYSTEM"
  24. IMAGE_KERNEL="KERNEL"
  25. REBOOT="0"
  26. # defaults for booting from an nbd root
  27. NBD_ROOT_SERVER="192.168.1.1"
  28. NBD_ROOT_PORT="2000"
  29. NFS_OVERLAY="192.168.1.1:/var/lib/overlay"
  30. # mount all needed special filesystems
  31. /bin/busybox mount -t devtmpfs none /dev
  32. /bin/busybox mount -t proc none /proc
  33. /bin/busybox mount -t sysfs none /sys
  34. # hide kernel log messages on console
  35. echo '1 4 1 7' > /proc/sys/kernel/printk
  36. # parse command line arguments
  37. for arg in $(cat /proc/cmdline); do
  38. case $arg in
  39. debugging)
  40. DEBUG=yes
  41. ;;
  42. nosplash)
  43. SPLASH=no
  44. ;;
  45. bootchart)
  46. BOOTCHART=yes
  47. ;;
  48. ssh)
  49. SSH=yes
  50. ;;
  51. progress)
  52. PROGRESS=yes
  53. ;;
  54. fastboot)
  55. FASTBOOT=yes
  56. ;;
  57. netboot)
  58. NETBOOT=yes
  59. ;;
  60. nbdroot=*)
  61. nbdroot="${arg#nbdroot=}"
  62. NBD_ROOT_SERVER=$( echo "${nbdroot}" | /bin/busybox sed 's/:.*//')
  63. NBD_ROOT_PORT=$( echo "${nbdroot}" | /bin/busybox sed 's/.*://')
  64. ;;
  65. nbdserver=*)
  66. NBD_ROOT_SERVER="${arg#nbdserver=}"
  67. ;;
  68. nbdport=*)
  69. NBD_ROOT_PORT="${arg#nbdport=}"
  70. ;;
  71. nfsoverlay=*)
  72. NFS_OVERLAY="${arg#nfsoverlay=}"
  73. ;;
  74. esac
  75. done
  76. if test "$FASTBOOT" = "yes"; then
  77. IONICE="/bin/busybox ionice -c 1 -n 0"
  78. fi
  79. progress() {
  80. if test "$PROGRESS" = "yes"; then
  81. echo "### $1 ###"
  82. fi
  83. }
  84. show_splash() {
  85. if [ ! -e /dev/fb0 ]; then
  86. SPLASH=no
  87. fi
  88. if [ -f /flash/oemsplash.png ]; then
  89. SPLASHIMAGE="/flash/oemsplash.png"
  90. elif [ -f /splash/splash.png ]; then
  91. SPLASHIMAGE="/splash/splash.png"
  92. else
  93. SPLASH=no
  94. fi
  95. if [ "$SPLASH" = "no" ]; then
  96. break
  97. else
  98. if [ -f "/bin/ply-image" ]; then
  99. /bin/ply-image "$SPLASHIMAGE" &
  100. fi
  101. fi
  102. }
  103. error() {
  104. echo "Error Code: $1 that means: $2"
  105. }
  106. debug_shell() {
  107. echo "### Starting debugging shell... type exit to quit ###"
  108. /bin/busybox sh </dev/tty1 >/dev/tty1 2>&1
  109. }
  110. mount_part() {
  111. # progress "check filesystem $1 ..."
  112. # /sbin/fsck -M -T -a $1 > /dev/null 2>&1
  113. for i in 1 2 3 4 5 6 7 8 9 10; do
  114. ERR_ENV=1
  115. MOUNT_OPTIONS="-o $3 $1 $2"
  116. if [ -n "$4" ]; then
  117. MOUNT_OPTIONS="-t $4 $MOUNT_OPTIONS"
  118. fi
  119. progress "mount filesystem $1 ..."
  120. $IONICE /bin/busybox mount $MOUNT_OPTIONS > /dev/null 2>&1
  121. [ "$?" -eq "0" ] && ERR_ENV=0 && break
  122. /bin/busybox usleep 1000000
  123. done
  124. [ "$ERR_ENV" -ne "0" ] && error "INIT_4" "Could not mount $1" && debug_shell
  125. }
  126. update() {
  127. if [ -f "$UPDATE_DIR/$2" ]; then
  128. echo "updating $1..."
  129. $IONICE /bin/busybox mount -o remount,rw /flash
  130. $IONICE /bin/busybox mv $UPDATE_DIR/$2 $3
  131. $IONICE /bin/busybox mount -o remount,ro /flash
  132. $IONICE /bin/busybox sync
  133. [ "$2" = "$IMAGE_KERNEL" ] && REBOOT="1"
  134. fi
  135. }
  136. hfsdiskprep() {
  137. for DEVICE in /dev/sd*; do
  138. for device in $(/bin/busybox blkid $DEVICE); do
  139. case $device in
  140. TYPE=*)
  141. FS_TYPE=${device#TYPE=}
  142. ;;
  143. esac
  144. done
  145. if [ "$FS_TYPE" = "\"hfs\"" -o "$FS_TYPE" = "\"hfsplus\"" ]; then
  146. progress "check filesystem $DEVICE [$FS_TYPE]..."
  147. /bin/fsck_hfs -r -y $DEVICE > /dev/null 2>&1
  148. fi
  149. done
  150. }
  151. mount_nbd() {
  152. retry_nr=0
  153. retry_delay=20
  154. OVERLAY_DIR=`cat /sys/class/net/eth0/address | /bin/busybox tr -d :`
  155. while [ ${retry_nr} -lt ${retry_delay} ] && [ ! -e /sysroot/sbin/init ]; do
  156. [ ${retry_nr} -gt 0 ] && \
  157. $IONICE /bin/busybox nbd-client $NBD_ROOT_SERVER $NBD_ROOT_PORT /dev/nbd0 && \
  158. mount_part "/dev/nbd0" "/sysroot" "ro" "squashfs"
  159. retry_nr=$(( ${retry_nr} + 1 ))
  160. [ ! -e /sysroot/sbin/init ] && /bin/busybox usleep 1000000
  161. [ ${retry_nr} -gt 0 ]
  162. done
  163. if [ ! -e /sysroot/sbin/init ]; then
  164. error "INIT_2" "Could not mount NBD root from $NBD_ROOT_SERVER port $NBD_ROOT_PORT"
  165. debug_shell
  166. fi
  167. mount_part "$NFS_OVERLAY" "/sysroot/storage" "rw,nolock,retrans=10" "nfs"
  168. if [ ! -d /sysroot/storage/$OVERLAY_DIR ]; then
  169. mkdir /sysroot/storage/$OVERLAY_DIR
  170. fi
  171. /bin/busybox umount /sysroot/storage
  172. mount_part "$NFS_OVERLAY/$OVERLAY_DIR" "/sysroot/storage" "rw,nolock" "nfs"
  173. }
  174. mount_disk() {
  175. # deal with hfs partitions
  176. if [ -x /sbin/fsck_hfs ]; then
  177. hfsdiskprep
  178. fi
  179. mount_part "$boot" "/flash" "ro,noatime"
  180. show_splash
  181. if [ -n "$disk" ]; then
  182. mount_part "$disk" "/storage" "rw,noatime"
  183. if [ -f "/flash/MACH_KERNEL" ]; then
  184. IMAGE_KERNEL="MACH_KERNEL"
  185. fi
  186. update "Kernel" "$IMAGE_KERNEL" "/flash/$IMAGE_KERNEL"
  187. update "System" "$IMAGE_SYSTEM" "/flash/$IMAGE_SYSTEM"
  188. if test "$REBOOT" -eq "1"; then
  189. echo "System reboots now..." && \
  190. /bin/busybox reboot
  191. fi
  192. fi
  193. if [ -f "/flash/$IMAGE_SYSTEM" ]; then
  194. mount_part "/flash/$IMAGE_SYSTEM" "/sysroot" "ro,loop"
  195. [ "$ERR_ENV" -ne "0" ] && debug_shell
  196. else
  197. error "INIT_2" "Could not find system."
  198. debug_shell
  199. fi
  200. # move /flash and /storage to /sysroot
  201. /bin/busybox mount --move /flash /sysroot/flash
  202. if [ -n "$disk" ]; then
  203. /bin/busybox mount --move /storage /sysroot/storage
  204. fi
  205. }
  206. if [ -z "$NETBOOT" ]; then
  207. mount_disk
  208. else
  209. mount_nbd
  210. fi
  211. # move some special filesystems
  212. /bin/busybox mount --move /dev /sysroot/dev
  213. /bin/busybox mount --move /proc /sysroot/proc
  214. /bin/busybox mount --move /sys /sysroot/sys
  215. # switch to new sysroot and start real init
  216. exec /bin/busybox switch_root /sysroot /sbin/init
  217. error "INIT_3" "Error in initramfs. Could not switch to new root"
  218. debug_shell