Advertisement
cyla

init

Nov 23rd, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.91 KB | None | 0 0
  1. #!/bin/busybox sh
  2.  
  3. rescue_shell() {
  4.     echo "$@"
  5.     echo "Something went wrong. Dropping you to a shell."
  6.     busybox --install -s
  7.     exec /bin/sh
  8. }
  9.  
  10. pretty_menu() {
  11.     num=0
  12.     while read menuitem ; do
  13.         if [ $num -gt 0 ] ; then
  14.             echo -n "${num}: "
  15.             echo $menuitem | cut -d "|" -f 4
  16.         else
  17.             defaultitem=$(echo $menuitem | cut -d "|" -f 1)
  18.             menutimeout=$(echo $menuitem | cut -d "|" -f 2)
  19.         fi
  20.         num=$(expr $num + 1)
  21.     done < /mnt/boot/menu.lst
  22.     echo
  23. }
  24.  
  25. read_selection() {
  26.     while [[ -z "${selecteditemindex}" || -z "${selectedline}" ]] ; do
  27.         read -t $menutimeout -p "Select an option (default is ${defaultitem} timeout is ${menutimeout}): " selecteditem || selecteditem=$defaultitem
  28.         if [[ $selecteditem -eq $selecteditem ]]; then
  29.             selecteditemindex=$(expr $selecteditem + 1)
  30.             selectedline=$(sed -n ${selecteditemindex}p /mnt/boot/menu.lst)
  31.             selectedtype=$(echo $selectedline | cut -d "|" -f 1)
  32.             selectedroot=$(echo $selectedline | cut -d "|" -f 2)
  33.             selectedoptions=$(echo $selectedline | cut -d "|" -f 3)
  34.             selectedname=$(echo $selectedline | cut -d "|" -f 4)
  35.         fi
  36.     done
  37. }
  38.  
  39. show_bootmenu() {
  40.     # mountimg /boot
  41.     mkdir -p /mnt/boot
  42.     mount /dev/mmcblk0p1 /mnt/boot || rescue_shell "Error while mounting /boot"
  43.  
  44.     # print boot menu
  45.     pretty_menu || rescue_shell "Error while printing boot menu"
  46.  
  47.     # read selected menu item
  48.     read_selection || rescue_shell "Error while reading selection"
  49.  
  50.     # umounting /boot
  51.     umount /mnt/boot || rescue_shell "Error while umounting /boot"
  52. }
  53.  
  54. activate_vgs() {
  55.     /sbin/pvscan --uuid --verbose || rescue_shell "Error while scanning physical volumes"
  56.     /sbin/vgscan --mknodes --verbose || rescue_shell "Error while scanning volume groups"
  57.  
  58.     for vg in $(/sbin/vgscan | grep "Found volume group" | cut -d\" -f2) ; do
  59.         echo "Activating volume group: $vg"
  60.         /sbin/vgchange -ay $vg --sysinit || rescue_shell "Error while activating $vg"
  61.         /sbin/vgmknodes --ignorelockingfailure $vg || rescue_shell "Error while making nodes for $vg"
  62.     done
  63. }
  64.  
  65. find_rootfs() {
  66.     if [ -z "${selectedoptions}" ] ; then
  67.         mountoptions="ro"
  68.     else
  69.         mountoptions="ro,${selectedoptions}"
  70.     fi
  71.     case $selectedtype in
  72.         phy|lvm)
  73.             fstype=$(echo $selectedroot | cut -d "=" -f 1)
  74.             if [ "${fstype}" == "LABEL" ] || [ "${fstype}" == "UUID" ] ; then
  75.                 fsname=$(echo $selectedroot | cut -d "=" -f 2)
  76.                 echo "Mounting rootfs: mount -o $mountoptions $(findfs "${fstype}=${fsname}") /mnt/root"
  77.                 mount -o $mountoptions $(findfs "${fstype}=${fsname}") /mnt/root
  78.                 return
  79.             else
  80.                 echo "Mounting rootfs: mount -o $mountoptions ${selectedroot} /mnt/root"
  81.                 mount -o $mountoptions $selectedroot /mnt/root
  82.                 return
  83.             fi
  84.         ;;
  85.         nfs)
  86.             echo "Mounting rootfs: mount -o ${selectedoptions} ${selectedroot} /mnt/root"
  87.             mount -o $mountoptions $selectedroot /mnt/root
  88.             return
  89.         ;;
  90.     esac
  91.  
  92.     rescue_shell "No root partition found"
  93. }
  94.  
  95. pretty_bootmsg() {
  96.     echo
  97.     echo "Booting ${selectedname} (${selectedroot})..."
  98. }
  99.  
  100. # temporarily mount proc and sys
  101. mount -t proc none /proc
  102. mount -t sysfs none /sys
  103. mount -t devtmpfs none /dev
  104.  
  105. # disable kernel messages from popping onto the screen
  106. echo 0 > /proc/sys/kernel/printk
  107.  
  108. # clear the screen
  109. clear
  110.  
  111. # display the boot menu
  112. show_bootmenu || rescue_shell "Error while reading boot menu"
  113.  
  114. # scan for vgs and activate each and every one of them
  115. activate_vgs || rescue_shell "Error while activating volume groups"
  116.  
  117. # mounting rootfs on /mnt/root
  118. find_rootfs || rescue_shell "Error while finding root filesystem"
  119.  
  120. # print boot message
  121. pretty_bootmsg || rescue_shell "Error while printing boot message"
  122.  
  123. # find and check mount points
  124. find_mountpoints || rescue_shell "Error while reading and checking mount points"
  125.  
  126. echo "All done. Switching to real root."
  127.  
  128. # clean up. The init process will remount proc sys and dev later
  129. umount /proc
  130. umount /sys
  131. umount /dev
  132.  
  133. # switch to the real root and execute init
  134. exec switch_root /mnt/root /sbin/init
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement