/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
- #!/bin/sh
- ################################################################################
- # This file is part of OpenELEC - http://www.openelec.tv
- # Copyright (C) 2009-2011 Stephan Raue (stephan@openelec.tv)
- # Copyright (C) 2010-2011 Roman Weber (roman@openelec.tv)
- #
- # This Program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2, or (at your option)
- # any later version.
- #
- # This Program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with OpenELEC.tv; see the file COPYING. If not, write to
- # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- # http://www.gnu.org/copyleft/gpl.html
- ################################################################################
- UPDATE_DIR=/storage/.update
- IMAGE_SYSTEM="SYSTEM"
- IMAGE_KERNEL="KERNEL"
- REBOOT="0"
- # defaults for booting from an nbd root
- NBD_ROOT_SERVER="192.168.1.1"
- NBD_ROOT_PORT="2000"
- NFS_OVERLAY="192.168.1.1:/var/lib/overlay"
- # mount all needed special filesystems
- /bin/busybox mount -t devtmpfs none /dev
- /bin/busybox mount -t proc none /proc
- /bin/busybox mount -t sysfs none /sys
- # hide kernel log messages on console
- echo '1 4 1 7' > /proc/sys/kernel/printk
- # parse command line arguments
- for arg in $(cat /proc/cmdline); do
- case $arg in
- debugging)
- DEBUG=yes
- ;;
- nosplash)
- SPLASH=no
- ;;
- bootchart)
- BOOTCHART=yes
- ;;
- ssh)
- SSH=yes
- ;;
- progress)
- PROGRESS=yes
- ;;
- fastboot)
- FASTBOOT=yes
- ;;
- netboot)
- NETBOOT=yes
- ;;
- nbdroot=*)
- nbdroot="${arg#nbdroot=}"
- NBD_ROOT_SERVER=$( echo "${nbdroot}" | /bin/busybox sed 's/:.*//')
- NBD_ROOT_PORT=$( echo "${nbdroot}" | /bin/busybox sed 's/.*://')
- ;;
- nbdserver=*)
- NBD_ROOT_SERVER="${arg#nbdserver=}"
- ;;
- nbdport=*)
- NBD_ROOT_PORT="${arg#nbdport=}"
- ;;
- nfsoverlay=*)
- NFS_OVERLAY="${arg#nfsoverlay=}"
- ;;
- esac
- done
- if test "$FASTBOOT" = "yes"; then
- IONICE="/bin/busybox ionice -c 1 -n 0"
- fi
- progress() {
- if test "$PROGRESS" = "yes"; then
- echo "### $1 ###"
- fi
- }
- show_splash() {
- if [ ! -e /dev/fb0 ]; then
- SPLASH=no
- fi
- if [ -f /flash/oemsplash.png ]; then
- SPLASHIMAGE="/flash/oemsplash.png"
- elif [ -f /splash/splash.png ]; then
- SPLASHIMAGE="/splash/splash.png"
- else
- SPLASH=no
- fi
- if [ "$SPLASH" = "no" ]; then
- break
- else
- if [ -f "/bin/ply-image" ]; then
- /bin/ply-image "$SPLASHIMAGE" &
- fi
- fi
- }
- error() {
- echo "Error Code: $1 that means: $2"
- }
- debug_shell() {
- echo "### Starting debugging shell... type exit to quit ###"
- /bin/busybox sh </dev/tty1 >/dev/tty1 2>&1
- }
- mount_part() {
- # progress "check filesystem $1 ..."
- # /sbin/fsck -M -T -a $1 > /dev/null 2>&1
- for i in 1 2 3 4 5 6 7 8 9 10; do
- ERR_ENV=1
- MOUNT_OPTIONS="-o $3 $1 $2"
- if [ -n "$4" ]; then
- MOUNT_OPTIONS="-t $4 $MOUNT_OPTIONS"
- fi
- progress "mount filesystem $1 ..."
- $IONICE /bin/busybox mount $MOUNT_OPTIONS > /dev/null 2>&1
- [ "$?" -eq "0" ] && ERR_ENV=0 && break
- /bin/busybox usleep 1000000
- done
- [ "$ERR_ENV" -ne "0" ] && error "INIT_4" "Could not mount $1" && debug_shell
- }
- update() {
- if [ -f "$UPDATE_DIR/$2" ]; then
- echo "updating $1..."
- $IONICE /bin/busybox mount -o remount,rw /flash
- $IONICE /bin/busybox mv $UPDATE_DIR/$2 $3
- $IONICE /bin/busybox mount -o remount,ro /flash
- $IONICE /bin/busybox sync
- [ "$2" = "$IMAGE_KERNEL" ] && REBOOT="1"
- fi
- }
- hfsdiskprep() {
- for DEVICE in /dev/sd*; do
- for device in $(/bin/busybox blkid $DEVICE); do
- case $device in
- TYPE=*)
- FS_TYPE=${device#TYPE=}
- ;;
- esac
- done
- if [ "$FS_TYPE" = "\"hfs\"" -o "$FS_TYPE" = "\"hfsplus\"" ]; then
- progress "check filesystem $DEVICE [$FS_TYPE]..."
- /bin/fsck_hfs -r -y $DEVICE > /dev/null 2>&1
- fi
- done
- }
- mount_nbd() {
- retry_nr=0
- retry_delay=20
- OVERLAY_DIR=`cat /sys/class/net/eth0/address | /bin/busybox tr -d :`
- while [ ${retry_nr} -lt ${retry_delay} ] && [ ! -e /sysroot/sbin/init ]; do
- [ ${retry_nr} -gt 0 ] && \
- $IONICE /bin/busybox nbd-client $NBD_ROOT_SERVER $NBD_ROOT_PORT /dev/nbd0 && \
- mount_part "/dev/nbd0" "/sysroot" "ro" "squashfs"
- retry_nr=$(( ${retry_nr} + 1 ))
- [ ! -e /sysroot/sbin/init ] && /bin/busybox usleep 1000000
- [ ${retry_nr} -gt 0 ]
- done
- if [ ! -e /sysroot/sbin/init ]; then
- error "INIT_2" "Could not mount NBD root from $NBD_ROOT_SERVER port $NBD_ROOT_PORT"
- debug_shell
- fi
- mount_part "$NFS_OVERLAY" "/sysroot/storage" "rw,nolock,retrans=10" "nfs"
- if [ ! -d /sysroot/storage/$OVERLAY_DIR ]; then
- mkdir /sysroot/storage/$OVERLAY_DIR
- fi
- /bin/busybox umount /sysroot/storage
- mount_part "$NFS_OVERLAY/$OVERLAY_DIR" "/sysroot/storage" "rw,nolock" "nfs"
- }
- mount_disk() {
- # deal with hfs partitions
- if [ -x /sbin/fsck_hfs ]; then
- hfsdiskprep
- fi
- mount_part "$boot" "/flash" "ro,noatime"
- show_splash
- if [ -n "$disk" ]; then
- mount_part "$disk" "/storage" "rw,noatime"
- if [ -f "/flash/MACH_KERNEL" ]; then
- IMAGE_KERNEL="MACH_KERNEL"
- fi
- update "Kernel" "$IMAGE_KERNEL" "/flash/$IMAGE_KERNEL"
- update "System" "$IMAGE_SYSTEM" "/flash/$IMAGE_SYSTEM"
- if test "$REBOOT" -eq "1"; then
- echo "System reboots now..." && \
- /bin/busybox reboot
- fi
- fi
- if [ -f "/flash/$IMAGE_SYSTEM" ]; then
- mount_part "/flash/$IMAGE_SYSTEM" "/sysroot" "ro,loop"
- [ "$ERR_ENV" -ne "0" ] && debug_shell
- else
- error "INIT_2" "Could not find system."
- debug_shell
- fi
- # move /flash and /storage to /sysroot
- /bin/busybox mount --move /flash /sysroot/flash
- if [ -n "$disk" ]; then
- /bin/busybox mount --move /storage /sysroot/storage
- fi
- }
- if [ -z "$NETBOOT" ]; then
- mount_disk
- else
- mount_nbd
- fi
- # move some special filesystems
- /bin/busybox mount --move /dev /sysroot/dev
- /bin/busybox mount --move /proc /sysroot/proc
- /bin/busybox mount --move /sys /sysroot/sys
- # switch to new sysroot and start real init
- exec /bin/busybox switch_root /sysroot /sbin/init
- error "INIT_3" "Error in initramfs. Could not switch to new root"
- debug_shell