update-motd-fsck-at-reboot 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh
  2. # Authors:
  3. # Mads Chr. Olesen <mads@mchro.dk>
  4. # Kees Cook <kees@ubuntu.com>
  5. set -e
  6. # poor mans force
  7. if [ "$1" = "--force" ]; then
  8. NEEDS_FSCK_CHECK=yes
  9. else
  10. if [ "$(id -u)" != 0 ] ; then
  11. exit
  12. fi
  13. NEED_FSCK_CHECK=no
  14. fi
  15. # check time when we did the last check
  16. stamp="/var/lib/update-notifier/fsck-at-reboot"
  17. if [ -e "$stamp" ]; then
  18. stampt=$(stat -c %Y $stamp)
  19. else
  20. stampt=0
  21. fi
  22. # check time when we last booted
  23. last_boot=$(date -d "now - $(awk '{print $1}' /proc/uptime) seconds" +%s)
  24. now=$(date +%s)
  25. if [ $(($stampt + 3600)) -lt $now ] || [ $stampt -gt $now ] \
  26. || [ $stampt -lt $last_boot ]
  27. then
  28. #echo $stampt $now need update
  29. NEEDS_FSCK_CHECK=yes
  30. fi
  31. # output something for update-motd
  32. if [ -n "$NEEDS_FSCK_CHECK" ]; then
  33. {
  34. check_occur_any=
  35. ext_partitions=$(mount | awk '$5 ~ /^ext(2|3|4)$/ { print $1 }')
  36. for part in $ext_partitions; do
  37. dumpe2fs_out=$(dumpe2fs -h $part 2>/dev/null)
  38. mount_count=$(echo "$dumpe2fs_out" | grep "^Mount count:"|cut -d':' -f 2-)
  39. if [ -z "$mount_count" ]; then mount_count=0; fi
  40. max_mount_count=$(echo "$dumpe2fs_out" | grep "^Maximum mount count:"|cut -d':' -f 2-)
  41. if [ -z "$max_mount_count" ]; then max_mount_count=0; fi
  42. check_interval=$(echo "$dumpe2fs_out" | grep "^Check interval:" | cut -d':' -f 2- | cut -d'(' -f 1)
  43. if [ -z "$check_interval" ]; then check_interval=0; fi
  44. next_check_date=$(echo "$dumpe2fs_out" | grep "^Next check after:" | cut -d':' -f 2-)
  45. if [ -z "$next_check_interval" ]; then next_check_interval=0; fi
  46. next_check_tstamp=$(date -d "$next_check_date" +%s)
  47. #echo "next_check_date=\"$next_check_date\" next_check_tstamp=\"$next_check_tstamp\""
  48. #echo "part=\"$part\" mount_count=\"$mount_count\" / max=\"$max_mount_count\" "
  49. check_occur=
  50. # Check based on mount counts?
  51. if [ "$max_mount_count" -gt 0 -a \
  52. "$mount_count" -ge "$max_mount_count" ]; then
  53. check_occur=yes
  54. fi
  55. # Check based on time passed?
  56. if [ "$check_interval" -gt 0 -a \
  57. "$next_check_tstamp" -lt "$now" ]; then
  58. check_occur=yes
  59. fi
  60. if [ -n "$check_occur" ]; then
  61. check_occur_any=yes
  62. mountpoint=$(mount | grep "^$part" | cut -d ' ' -f 3)
  63. pass=$(grep -v '^#' /etc/fstab | tr -s ' ' '\t' | cut -s -f 2,6 | grep -w "$mountpoint" | cut -f 2)
  64. if [ "$pass" = "0" ]; then
  65. echo "*** $part should be checked for errors ***"
  66. else
  67. echo "*** $part will be checked for errors at next reboot ***"
  68. fi
  69. fi
  70. done
  71. if [ -n "$check_occur_any" ]; then
  72. echo ""
  73. fi
  74. } > $stamp
  75. fi
  76. # output what we have (either cached or newly generated)
  77. cat $stamp