How to tell if a disk needs fsck ...

July 28th, 2012 - 03:09 pm ET by Huge | Report spam
... from a script.

I'd like my backup script to know if the exchangeable disk I've just
inserted is overdue an fsck. Is there any way to do that from a shell
script, other than grepping the kernel syslog?

Today is Prickle-Prickle, the 63rd day of Confusion in the YOLD 3178
"If there is anyone here who I have not insulted, I beg their
pardon." Johannes Brahms (1833-97).
email Follow the discussionReplies 4 repliesReplies Make a reply

Similar topics

Replies

#1 J G Miller
July 28th, 2012 - 03:41 pm ET | Report spam
On Saturday, July 28th, 2012, at 19:09:13h +0000, Huge asked:

I'd like my backup script to know if the exchangeable disk I've just
inserted is overdue an fsck. Is there any way to do that from a shell
script, other than grepping the kernel syslog?



Well it never hurts to fsck even if it is not "officialy required".

But to answer your question, you have not stated which type of
file system are you wanting to check.

Making the assumption based on probability that you are asking about
ext2/ext3/ext4, I will attempt an answer related to that type of
filesystem, so please refrain from legal action if that is an
incorrect assumption.

You can use the blkid command to determine the type of file system
which a partition contains.

fs_type="`blkid ${device} | sed -ne 's|.*TYPE=\"\([^\"]*\)\".*|\1|p'`"

where device is /dev/sdb7, /dev/sdc1 etc

An ext{2,3,4} filesystem officially requires an automatic fsck when
one of two conditions are met

1) the maximal mount count has been reached since last fsck

2) the time since last fsck has exceeded the fsck maximal elaspse time

Both of these parameters are set (from default values) when the
file system is initially created and can by modified by the
tune2fs program.

The tune2fs program will also list their values

tune2fs -l /dev/sd{drive_letter}{partition_number}

eg tune2fs -l /dev/sdb7

So the entries you need to consider are

Mount count: 11
Maximum mount count: 24

and

Last mount time: Tue Jul 24 12:56:11 2012
Last write time: Tue Jul 24 14:42:13 2012

and

Check interval: 15552000 (6 months)
Next check after: Sun Dec 9 01:50:01 2012

Thus an fsck is "officialy required" if maximum mount count
is equal to or greater than mount count, or if check interval is
greater than "last mount time" or "last write time".

Most importantly you must check that the filesystem is not
mounted before trying to run the fsck program, eg with

mount | egrep "${device}"
Replies Reply to this message
#2 Jim Price
July 28th, 2012 - 03:45 pm ET | Report spam
On 28/07/12 20:09, Huge wrote:
... from a script.

I'd like my backup script to know if the exchangeable disk I've just
inserted is overdue an fsck. Is there any way to do that from a shell
script, other than grepping the kernel syslog?



The mount count is a part of the output of tune2fs -l /dev/sda<n>. The
maximum mount count is also in that output, so it's overdue if the first
is greater.

╔═╦═╦═════╦═══╗
║ ║ ║ ║ ║
╔═╝ ║ ║ ║ ║ ║ ╔═╝
╚═══╩═╩═╩═╩═╩═╝ -- JimP.
Replies Reply to this message
#3 Huge
July 28th, 2012 - 05:18 pm ET | Report spam
On 2012-07-28, Jim Price wrote:
On 28/07/12 20:09, Huge wrote:
... from a script.

I'd like my backup script to know if the exchangeable disk I've just
inserted is overdue an fsck. Is there any way to do that from a shell
script, other than grepping the kernel syslog?



The mount count is a part of the output of tune2fs -l /dev/sda<n>. The
maximum mount count is also in that output, so it's overdue if the first
is greater.



Brilliant, thank you!


Today is Prickle-Prickle, the 63rd day of Confusion in the YOLD 3178
"If there is anyone here who I have not insulted, I beg their
pardon." Johannes Brahms (1833-97).
Replies Reply to this message
#4 J G Miller
July 28th, 2012 - 07:10 pm ET | Report spam
On Saturday, July 28th, 2012, at 21:18:13h +0000, Huge wrote:

On 2012-07-28, Jim Price wrote:
On 28/07/12 20:09, Huge wrote:
... from a script.

I'd like my backup script to know if the exchangeable disk I've just
inserted is overdue an fsck. Is there any way to do that from a shell
script, other than grepping the kernel syslog?



The mount count is a part of the output of tune2fs -l /dev/sda<n>. The
maximum mount count is also in that output, so it's overdue if the first
is greater.



Brilliant, thank you!



He did not mention the other condition about time elapsed since last fsck
which I did explain in detail in my posting, which is also available
from tune2fs -l.
email Follow the discussion Replies Reply to this message
Help Create a new topicReplies Make a reply
Search Make your own search