Automatically force fsck after some number of reboots

Aug 14, '03 09:54:00AM

Contributed by: nobody

Since I have used the journaled filesystem on my Mac for the past several months, fsck probably hasn't been executed since before I switched. Yesterday some problems occured, like missing icons, and beeing unable to drag and drop with some applications. After doing an fsck, everything was OK. So, even if you have a journaled file system, corruption might occur during normal usage, not because of a crash. Why not let the system automatically fsck your root volume every N restarts?

Enclosed a simple script wich will do this. To use it, you must modify your /etc/rc file as root or with sudo. Make a backup before changing in case you do something wrong. Read the rest of the script for the how-to...

[robg adds: Anytime you're editing a system file such as rc, you should definitely have a backup (and a way to get to it!). A mistake might cause havoc on your next boot. I have not tested this hint.]

Insert the script at the line that reads ConsoleMessage "Mounting local filesystems". The line, where the amount of restarts is saved to the disk, is after mkdir -p -m 0555 /.vol && chmod 0555 /.vol && mount_volfs /.vol. So you have to remove a few dupliate lines at the bottom. If you want to force a check at the next startup do a sudo rm /var/tmp/mountcount.

I checked the script now several times and it looks like it's doing what it should. However, all mods are your own risk. The Script fragment:


ConsoleMessage "Mounting local filesystems"

##  Modification of the startupscript to do a fsck even if the
##  volume is clean after every MAXMOUNT restarts
MAXMOUNT=10

if [ -f /var/tmp/mountcount ]; then
  COUNT=$(/bin/cat /var/tmp/mountcount)
  NEWCOUNT=`expr $COUNT + 1`
  /bin/echo "Mount count: $COUNT "
  if [ "$COUNT" -gt "$MAXMOUNT" ]; then
    ConsoleMessage "Maximum mount count reached, checking of filesystem forced"
    echo "max mount reached"
    /sbin/fsck -fy
    NEWCOUNT=1
  fi
else
  NEWCOUNT=1
  ConsoleMessage "First time mounting local Volumes, or no reboot since a long time, check forced"
  /sbin/fsck -fy
fi

mount -vat hfs
mount -vat ufs
mount -t fdesc -o union stdin /dev
mkdir -p -m 0555 /.vol && chmod 0555 /.vol && mount_volfs /.vol

## done mount and checking, write the amount of mounts/restarts

/bin/echo $NEWCOUNT >/var/tmp/mountcount

##end of modifications

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=200308101049258