Advertisement
cyla

init

Jan 6th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.99 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. activate_vgs() {
  11.     /sbin/vgscan --mknodes
  12.     for vg in $(/sbin/vgscan | grep "Found volume group" | cut -d\" -f2) ; do
  13.         echo "Activating vg: $vg"
  14.         /sbin/vgchange -ay $vg || rescue_shell "Error while activating $vg"
  15.     done
  16. }
  17.  
  18. uuidlabel_root() {
  19.     for cmd in $(cat /proc/cmdline) ; do
  20.         case $cmd in
  21.             root=*)
  22.                 type=$(echo $cmd | cut -d= -f2)
  23.                 echo "Mounting rootfs"
  24.                 if [ $type == "LABEL" ] || [ $type == "UUID" ] ; then
  25.                     uuid=$(echo $cmd | cut -d= -f3)
  26.                     mount -o ro $(findfs "$type"="$uuid") /mnt/root
  27.                 else
  28.                     mount -o ro $(echo $cmd | cut -d= -f2) /mnt/root
  29.                 fi
  30.             ;;
  31.         esac
  32.     done
  33.     rescue_shell "No root partition found"
  34. }
  35.  
  36. check_filesystem() {
  37.     # most of code coming from /etc/init.d/fsck
  38.  
  39.     local fsck_opts= check_extra= RC_UNAME=$(uname -s)
  40.  
  41.     # FIXME : get_bootparam forcefsck
  42.     if [ -e /forcefsck ]; then
  43.         fsck_opts="$fsck_opts -f"
  44.         check_extra="(check forced)"
  45.     fi
  46.  
  47.     echo "Checking local filesystem $check_extra : $1"
  48.  
  49.     if [ "$RC_UNAME" = Linux ]; then
  50.         fsck_opts="$fsck_opts -C0 -T"
  51.     fi
  52.  
  53.     trap : INT QUIT
  54.  
  55.     # using our own fsck, not the builtin one from busybox
  56.     /sbin/fsck -p $fsck_opts $1
  57.  
  58.     case $? in
  59.         0)      return 0;;
  60.         1)      echo "Filesystem repaired"; return 0;;
  61.         2|3)    if [ "$RC_UNAME" = Linux ]; then
  62.                         echo "Filesystem repaired, but reboot needed"
  63.                         reboot -f
  64.                 else
  65.                         rescue_shell "Filesystem still have errors; manual fsck required"
  66.                 fi;;
  67.         4)      if [ "$RC_UNAME" = Linux ]; then
  68.                         rescue_shell "Fileystem errors left uncorrected, aborting"
  69.                 else
  70.                         echo "Filesystem repaired, but reboot needed"
  71.                         reboot
  72.                 fi;;
  73.         8)      echo "Operational error"; return 0;;
  74.         12)     echo "fsck interrupted";;
  75.         *)      echo "Filesystem couldn't be fixed";;
  76.     esac
  77.     rescue_shell
  78. }
  79.  
  80. # temporarily mount proc and sys
  81. mount -t proc none /proc
  82. mount -t sysfs none /sys
  83. mount -t devtmpfs none /dev
  84.  
  85. # disable kernel messages from popping onto the screen
  86. echo 0 > /proc/sys/kernel/printk
  87.  
  88. # clear the screen
  89. clear
  90.  
  91. # scan for vgs and activate each and every one of them
  92. activate_vgs
  93.  
  94. # mounting rootfs on /mnt/root
  95. uuidlabel_root || rescue_shell "Error with uuidlabel_root"
  96.  
  97. # space separated list of mountpoints that ...
  98. mountpoints=""
  99.  
  100. # ... we want to find in /etc/fstab ...
  101. ln -s /mnt/root/etc/fstab /etc/fstab
  102.  
  103. # ... to check filesystems and mount our devices.
  104. for m in $mountpoints ; do
  105.     check_filesystem $m
  106.  
  107.     echo "Mounting $m"
  108.     # mount the device and ...
  109.     mount $m || rescue_shell "Error while mounting $m"
  110.  
  111.     # ... move the tree to its final location
  112.     mount --move $m "/mnt/root"$m || rescue_shell "Error while moving $m"
  113. done
  114.  
  115. echo "All done. Switching to real root."
  116.  
  117. # clean up. The init process will remount proc sys and dev later
  118. umount /proc
  119. umount /sys
  120. umount /dev
  121.  
  122. # switch to the real root and execute init
  123. exec switch_root /mnt/root /sbin/init
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement