Advertisement
Guest User

bear24rw

a guest
Jun 10th, 2009
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.14 KB | None | 0 0
  1. #
  2. # checkroot.sh  Check to root filesystem.
  3. #
  4. # Version:  @(#)checkroot.sh  2.84  25-Jan-2002  miquels@cistron.nl
  5. #
  6.  
  7. ROOTDEV=/dev/mmcblk0p2
  8. if [ ! -b $ROOTDEV ]; then
  9.     echo "$ROOTDEV not found or not a device file"
  10.     exit 0
  11. fi
  12. ROOTTYPE_AUTO=ext3
  13.  
  14.  
  15. # Shortcut to do nothing (workaround for NOV-10897)
  16. # (but only if the rootfs is already rw, as per kernel cmdline)
  17. # (qemu still needs checkroot.sh to remount root as rw)
  18. if grep -qs rw /proc/cmdline ; then
  19.     # rootfs already rw as per kernel cmdline; skip checkroot
  20.     exit 0
  21. fi
  22.  
  23. # rw NOT detected in kernel cmdline
  24. # Run checkroot to remount rootfs rw...
  25.  
  26. . /etc/default/rcS
  27.  
  28. #
  29. # Read /etc/fstab.
  30. #
  31. exec 9>&0 </etc/fstab
  32. rootmode=rw
  33. rootopts=rw
  34. # Set rootcheck without bothering with /etc/default/rcS
  35. rootcheck="yes"
  36. swap_on_md=no
  37. devfs=
  38. while read fs mnt type opts dump pass junk
  39. do
  40.     case "$fs" in
  41.         ""|\#*)
  42.             continue;
  43.             ;;
  44.         /dev/md*)
  45.             # Swap on md device.
  46.             test "$type" = swap && swap_on_md=yes
  47.             ;;
  48.         /dev/*)
  49.             ;;
  50.         *)
  51.             # Might be a swapfile.
  52.             test "$type" = swap && swap_on_md=yes
  53.             ;;
  54.     esac
  55.    
  56.     test "$type" = devfs && devfs="$fs"
  57.  
  58.     # Currently we do not care about the other entries
  59.     if test "$mnt" = "/"
  60.     then
  61.         #echo "[$fs] [$mnt] [$type] [$opts] [$dump] [$pass] [$junk]"
  62.  
  63.         rootopts="$opts"       
  64.         roottype="$type"
  65.  
  66.         #The "spinner" is broken on busybox sh 
  67.         TERM=dumb
  68.            
  69.         test "$pass" = 0 -o "$pass" = "" && rootcheck=no
  70.        
  71.         # Enable fsck for ext2 and ext3 rootfs, disable for everything else
  72.         # Allow auto as well -- a hack
  73.         case "$type" in
  74.             auto)
  75.                 roottype=$ROOTTYPE_AUTO
  76.                 rootcheck=yes
  77.                 ;;
  78.             ext2|ext3) 
  79.                 rootcheck=yes
  80.                 ;;
  81.             *) 
  82.                 rootcheck=no
  83.                 ;;
  84.         esac
  85.        
  86.         if test "$rootcheck" = yes
  87.         then
  88.             if ! test -x "/sbin/fsck.${roottype}"
  89.             then
  90.                 echo -e "\n * * * WARNING: /sbin/fsck.${roottype} is missing! * * *\n"
  91.                 rootcheck=no
  92.             fi
  93.         fi
  94.        
  95.         case "$opts" in
  96.             ro|ro,*|*,ro|*,ro,*)
  97.                 rootmode=ro
  98.                 ;;
  99.         esac
  100.     fi
  101. done
  102. exec 0>&9 9>&-
  103.  
  104. #
  105. # Check the root filesystem.
  106. #
  107. if test -f /fastboot || test $rootcheck = no
  108. then
  109.   test $rootcheck = yes && echo "Fast boot, no filesystem check"
  110. else
  111.   #
  112.   # Ensure that root is quiescent and read-only before fsck'ing.
  113.   #
  114.   mount -n -o remount,ro /
  115.   if test $? = 0
  116.   then
  117.     if test -f /forcefsck
  118.     then
  119.         force="-f"
  120.     else
  121.         force=""
  122.     fi
  123.     if test "$FSCKFIX" = yes
  124.     then
  125.         fix="-y"
  126.     else
  127.         fix="-a"                # mapped to -p for ext2
  128.     fi
  129.     spinner="-C"
  130.     case "$TERM" in
  131.         dumb|network|unknown|"") spinner="" ;;
  132.     esac
  133.     test `uname -m` = s390 && spinner="" # This should go away
  134.     test "$VERBOSE" != no && echo "Checking root filesystem..."
  135.     fsck $spinner $force $fix $ROOTDEV
  136.     RTC=$?
  137.     #
  138.     # If there was a failure, drop into single-user mode.
  139.     #
  140.     # NOTE: "failure" is defined as exiting with a return code of
  141.     # 2 or larger.  A return code of 1 indicates that filesystem
  142.     # errors were corrected but that the boot may proceed.
  143.     #
  144.    
  145.     echo "RETURNCODE: [$RTC]"
  146.    
  147.     if test "$RTC" -gt 3
  148.     then
  149.    
  150.       # Since this script is run very early in the boot-process, it should be safe to assume that the
  151.       # output is printed to VT1. However, some distributions use a bootsplash to hide the "ugly" boot
  152.       # messages and having the bootsplash "hang" due to a waiting fsck prompt is less than ideal
  153.       chvt 1
  154.    
  155.       # Surprise! Re-directing from a HERE document (as in
  156.       # "cat << EOF") won't work, because the root is read-only.
  157.       echo
  158.       echo "fsck failed.  Please repair manually and reboot. "
  159.       echo "Please note that the root filesystem is currently "
  160.       echo "mounted read-only.  To remount it read-write:"
  161.       echo
  162.       echo "   # mount -n -o remount,rw /"
  163.       echo
  164.       echo "CONTROL-D will exit from this shell"
  165.       echo "and REBOOT the system."
  166.       echo
  167.       # Start a single user shell on the console
  168.       test -x /sbin/sulogin && /sbin/sulogin $CONSOLE
  169.       reboot -f
  170.     fi
  171.   else
  172.     echo "*** ERROR!  Cannot fsck root fs because it is not mounted read-only!"
  173.     echo
  174.   fi
  175. fi
  176.  
  177. #
  178. #   If the root filesystem was not marked as read-only in /etc/fstab,
  179. #   remount the rootfs rw but do not try to change mtab because it
  180. #   is on a ro fs until the remount succeeded. Then clean up old mtabs
  181. #   and finally write the new mtab.
  182. #   This part is only needed if the rootfs was mounted ro.
  183. #
  184. ROOTFSDEV="/dev/root"
  185. if ! grep -q "^$ROOTFSDEV " /proc/mounts; then
  186.   ROOTFSDEV="rootfs"
  187. fi
  188. if [ $(grep "^$ROOTFSDEV .*static" /proc/mounts | awk '{print $4}' | sed 's/,.*$//') = rw ]; then
  189.     echo "Root filesystem already read-write, not remounting"
  190.     exit 0
  191. fi
  192.  
  193. echo "Remounting root file system..."
  194. mount -n -o remount,$rootmode /
  195. if test "$rootmode" = rw
  196. then
  197.     if test ! -L /etc/mtab
  198.     then
  199.         rm -f /etc/mtab~ /etc/nologin
  200.         : > /etc/mtab
  201.     fi
  202.     mount -f -o remount /
  203.     mount -f /proc
  204.     test "$devfs" && grep -q '^devfs /dev' /proc/mounts && mount -f "$devfs"
  205. fi
  206.  
  207. : exit 0
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement