#!/bin/sh # # Copyright (c) 2010 Adam Cécile (Le_Vert) <gandalf@le-vert.net> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # How to use it: # # Copy this file to /usr/bin # Create aliases for all commands that may make you suffer from fsck. # # In example, on Debian, add the following lines to /etc/bash.bashrc # and /etc/profile: # # Reboot/Halt/Shutdown aliases # alias shutdown='/usr/bin/fsck-or-not.sh shutdown' # alias halt='/usr/bin/fsck-or-not.sh halt' # alias reboot='/usr/bin/fsck-or-not.sh reboot' # # If you want to make it works with sudo too, edit /etc/bash.bashrc # and /etc/profile again to add: # alias sudo='sudo ' # # Here is an extract from bash manpage that explains the needs of # this sudo alias: # If the last character of the alias value is a blank, then the next # command word following the alias is also checked for alias expansion. # # Each time you tape either shutdown, halt or reboot, this script # will check if any currently mounted EXT filesystem requires fsck # and thus, will ask you for confirmation before running the real # command. # Initialize a global variable set to 1 if at least fs needs fsck globalneedfsck=0 # Check currently mounted ext2/3/4 filesystems for fs in `mount | grep -E ' ext[234] ' | cut -d' ' -f1`; do # Initialize a variable to store fs state needfsck=0 # Get filesystem state state=`tune2fs -l ${fs} | grep -E '^Filesystem state:[[:space:]]+' | cut -d':' -f2 | sed 's/[[:space:]]*//'` # Get current mount count mountcount=`tune2fs -l ${fs} | grep -E '^Mount count:[[:space:]]+' | cut -d':' -f2 | sed 's/[[:space:]]*//'` # Get maximum mount count maximummountcount=`tune2fs -l ${fs} | grep -E '^Maximum mount count:[[:space:]]+' | cut -d':' -f2 | sed 's/[[:space:]]*//'` # Get next check date nextcheck=`tune2fs -l ${fs} | grep -E '^Next check after:[[:space:]]+' | awk -F': ' '{ print $2 }' | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//'` # Get last mount date (to check "last mount in future) lastmount=`tune2fs -l ${fs} | grep -E '^Last mount time:[[:space:]]+' | awk -F': ' '{ print $2 }' | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//'` # Convert next check date to timestamp nextchecktimestamp=`LANG=C date "+%s" -d "${nextcheck}"` # Convert last mount time into timestamp lastmounttimestamp=`LANG=C date "+%s" -d "${lastmount}"` # Get current date as timestamp and add 10 minutes (time to stop+time to boot) nowtimestamp=`LANG=C date -d "+10 minutes" "+%s"` # Get current mount point mountpoint=`mount | grep -E "^${fs}" | sed 's/^.* on \(.*\) type .*$/\1/'` # Here we go... # State is not clean => fsck if [ "${state}" != "clean" ]; then needfsck=1 fi # Current mount count >= maximum mount count => fsck if [ ${mountcount} -ge ${maximummountcount} ]; then needfsck=1 fi # Current date +10minutes >= next check date => fsck if [ ${nowtimestamp} -ge ${nextchecktimestamp} ]; then needfsck=1 fi # Last mount time in future => fsck if [ ${lastmounttimestamp} -gt ${nowtimestamp} ]; then needfsck=1 fi # Print a warning and set global variable to 1 if [ ${needfsck} -eq 1 ]; then echo "Device ${fs} mounted on ${mountpoint} will be checked at next boot." globalneedfsck=1 fi done # Global warning and confirmation question if [ ${globalneedfsck} -eq 1 ]; then echo echo "At least one filesystem will be check at next boot." echo "Are you sure you want to continue? (N/y)" read answer case ${answer} in y* | Y* ) # Exec parameters exec "$@" ;; esac # No fsck needed, run the command passed as argument else exec "$@" fi
|