Advertisement
cyla

init

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