checkroot.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: checkroot
  4. # Required-Start: udev
  5. # Required-Stop:
  6. # Default-Start: S
  7. # Default-Stop:
  8. # Short-Description: Check to root file system.
  9. ### END INIT INFO
  10. . /etc/default/rcS
  11. #
  12. # Set SULOGIN in /etc/default/rcS to yes if you want a sulogin to be spawned
  13. # from this script *before anything else* with a timeout, like SCO does.
  14. #
  15. test "$SULOGIN" = yes && sulogin -t 30 $CONSOLE
  16. #
  17. # Read /etc/fstab.
  18. #
  19. exec 9< /etc/fstab
  20. rootmode=rw
  21. rootopts=rw
  22. rootcheck=$ENABLE_ROOTFS_FSCK
  23. swap_on_md=no
  24. devfs=
  25. while read fs mnt type opts dump pass junk <&9
  26. do
  27. case "$fs" in
  28. ""|\#*)
  29. continue;
  30. ;;
  31. /dev/md*)
  32. # Swap on md device.
  33. test "$type" = swap && swap_on_md=yes
  34. ;;
  35. /dev/*)
  36. ;;
  37. *)
  38. # Might be a swapfile.
  39. test "$type" = swap && swap_on_md=yes
  40. ;;
  41. esac
  42. test "$type" = devfs && devfs="$fs"
  43. test "$mnt" != / && continue
  44. rootopts="$opts"
  45. test "$pass" = 0 -o "$pass" = "" && rootcheck=no
  46. case "$opts" in
  47. ro|ro,*|*,ro|*,ro,*)
  48. rootmode=ro
  49. ;;
  50. esac
  51. done
  52. exec 0>&9 9>&-
  53. # Check for conflicting configurations
  54. if [ "$rootmode" = "ro" -a "$ROOTFS_READ_ONLY" = "no" ] || \
  55. [ "$rootmode" = "rw" -a "$ROOTFS_READ_ONLY" = "yes" ]; then
  56. echo ""
  57. echo "WARN: conflicting configurations in /etc/fstab and /etc/default/rcS"
  58. echo " regarding the writability of rootfs. Please fix one of them."
  59. echo ""
  60. fi
  61. #
  62. # Activate the swap device(s) in /etc/fstab. This needs to be done
  63. # before fsck, since fsck can be quite memory-hungry.
  64. #
  65. test "$VERBOSE" != no && echo "Activating swap"
  66. [ -x /sbin/swapon ] && swapon -a
  67. #
  68. # Check the root filesystem.
  69. #
  70. if test -f /fastboot || test $rootcheck = no
  71. then
  72. test $rootcheck = yes && echo "Fast boot, no filesystem check"
  73. else
  74. #
  75. # Ensure that root is quiescent and read-only before fsck'ing.
  76. #
  77. mount -n -o remount,ro /
  78. if test $? = 0
  79. then
  80. if test -f /forcefsck
  81. then
  82. force="-f"
  83. else
  84. force=""
  85. fi
  86. if test "$FSCKFIX" = yes
  87. then
  88. fix="-y"
  89. else
  90. fix="-a"
  91. fi
  92. spinner="-C"
  93. case "$TERM" in
  94. dumb|network|unknown|"") spinner="" ;;
  95. esac
  96. test `uname -m` = s390 && spinner="" # This should go away
  97. test "$VERBOSE" != no && echo "Checking root filesystem..."
  98. fsck $spinner $force $fix /
  99. #
  100. # If there was a failure, drop into single-user mode.
  101. #
  102. # NOTE: "failure" is defined as exiting with a return code of
  103. # 2 or larger. A return code of 1 indicates that filesystem
  104. # errors were corrected but that the boot may proceed.
  105. #
  106. if test "$?" -gt 1
  107. then
  108. # Surprise! Re-directing from a HERE document (as in
  109. # "cat << EOF") won't work, because the root is read-only.
  110. echo
  111. echo "fsck failed. Please repair manually and reboot. Please note"
  112. echo "that the root filesystem is currently mounted read-only. To"
  113. echo "remount it read-write:"
  114. echo
  115. echo " # mount -n -o remount,rw /"
  116. echo
  117. echo "CONTROL-D will exit from this shell and REBOOT the system."
  118. echo
  119. # Start a single user shell on the console
  120. /sbin/sulogin $CONSOLE
  121. reboot -f
  122. fi
  123. else
  124. echo "*** ERROR! Cannot fsck root fs because it is not mounted read-only!"
  125. echo
  126. fi
  127. fi
  128. #
  129. # If the root filesystem was not marked as read-only in /etc/fstab,
  130. # remount the rootfs rw but do not try to change mtab because it
  131. # is on a ro fs until the remount succeeded. Then clean up old mtabs
  132. # and finally write the new mtab.
  133. #
  134. mount -n -o remount,$rootmode /
  135. if test "$rootmode" = rw
  136. then
  137. ln -sf /proc/mounts /dev/mtab
  138. fi
  139. : exit 0