PageRenderTime 36ms CodeModel.GetById 9ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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