Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatically force fsck after some number of reboots System
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

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
  ConsoleMessage "First time mounting local Volumes, or no reboot since a long time, check forced"
  /sbin/fsck -fy

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
  • Currently 2.33 / 5
  You rated: 3 / 5 (3 votes cast)

Automatically force fsck after some number of reboots | 7 comments | Create New Account
Click here to return to the 'Automatically force fsck after some number of reboots' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically force fsck after some number of reboots
Authored by: gmachen on Aug 14, '03 12:33:40PM

According to Apple, apparently one is not supposed to do an fsck when using journaling. It gives false alarms that don't need repairing. Moreover, if fsck is run anyway, the flag that prevents affecting any repairs is supposed to be set.

Am I interpreting this correctly?

[ Reply to This | # ]
Automatically force fsck after some number of reboots
Authored by: Anonymous on Aug 14, '03 01:48:53PM

Yes, you are not supposed to run fsck with journaling on.

In other words, journaling (supposedly) prevents the errors that fsck would have to fix.

[ Reply to This | # ]
fsck done in Safe Boot mode
Authored by: hayne on Aug 14, '03 02:47:01PM

'd like to point out that when you restart in "Safe Boot" mode (hold down the Shift key while restarting), the script /etc/rc.boot does an 'fsck -fy' automatically.

This means two things:
1) There is nothing wrong with doing 'fsck -fy' even if journalling is turned on.
2) A possibly safer (though certainly less convenient) method for forcing the fsck is to do a Safe Boot once in a while. After doing the Safe Boot, you should immediately restart again (without holding Shift) since some things are disabled when in Safe Boot mode.

I don't see anything wrong with the modifications (but I'm not going to test this on my system) and if something does go wrong, I think you should be able to boot into Single User mode (holding Command + S at restart) and restore the /etc/rc script from your backup copy (e.g. /etc/rc.original)

[ Reply to This | # ]
fsck versus fsck_hfs
Authored by: SOX on Aug 14, '03 03:57:28PM

I thouhgt on apple's we were supposed to use fsck_hfs

[ Reply to This | # ]
Re: fsck versus fsck_hfs
Authored by: jiclark on Aug 16, '03 01:42:52AM
Its a funny thing about this place. I can't remember how many times a really interesting question like this gets asked.... and nobody responds! I never cease to wonder if the Unix 'experts' actually seek to perpetuate the "mystique'?!? ;-}


[ Reply to This | # ]

Re: fsck versus fsck_hfs
Authored by: nobody on Aug 16, '03 12:33:01PM

If you call fsck, it will, if the program is able to determine that the volume you want to repair/check is a hfs volume, it will call fsck_hfs.
Do a ls -l /sbin/fsck* and you will see.
If you have a linux i386, there will be more versions of fsck*, depending non your installed filesystems in the kernel. OSX currently only supports hfs and "msdos" - FAT. Panther will introduce ntfs. There is some work ongoing at sourceforge to support EXT2 as a kernel extension in OSX.
So, it does not matter if you call fsck.

[ Reply to This | # ]
Re: fsck versus fsck_hfs
Authored by: sjk on Aug 18, '03 08:19:53PM
Do a ls -l /sbin/fsck* and you will see.

Actually, running strings /sbin/fsck | fgrep hfs is more supportive of your statement. :-)

[ Reply to This | # ]