Thursday, March 31, 2016

Check Raspbian Filesystem/Disk on Raspberry Pi

How do I check my Pi's (root) filesystem?

There's often a certain air in the voice of the experienced Linux users when asked a question like this; "How do I check the filesystem on my Linux system?"

The tool to use is fsck, the filesystem checker. The problem is that if you're checking a mounted filesystem, fsck will warn you vehemently that you can and will risk destroying your filesystem when checking a mounted device.

And you, stupid user, are asking this all-too-common question when you should have RTFM'd.

It turns out that the answer to the question is to log in to the Pi and run this at the terminal:

sudo touch /forcefsck

Once the system comes back up, you can look for the results in /var/log/daemon.log.

If you did try to RTFM, there's a number of sites that say you should run shutdown with a "-F" parameter. At some point that was removed from the shutdown command, yielding a somewhat cryptic error about unhandled options and leaving behind a LOT of documentation cruft that won't work. After doing some digging, it looks like the -F did automatically what the touch command above does; it was decided that if your filesystem is in a yarked state, writing an empty file to the filesystem would be possibly more damaging than helpful. From the bug report:

If you want to force an fsck on next boot, please add fsck.mode=force to
the kernel command line (see man systemd-fsck@.service).

systemd intentionally doesn't write a /force.fsck flag file to the disk
to achieve that. You typically want to avoid writing to a file system if
you are concerned about it's consistency. That's why the -F parameter is
not supported (and won't be added).
So that's the "official" fix.

There's also a setting found in /etc/Default/rcS (a text file) where you change "FSCKFIX=no" to yes and your system should check the root filesystem at each reboot.

How do I know when the filesystem on my Pi was last checked?

This involves the use of tune2fs. Run

mount

...to get the /dev device name of the filesystem you want to check. For me, it said /dev/mmcblk0p2 was mounted on /. Then I ran:

sudo tune2fs -l /dev/mmcblk0p2|grep Last\ c

... to get the time and date the filesystem was last checked.

Forcing a check on non-root partitions isn't the same as above; you can't use /forcefsck on the partitions to auto-check it. It instead involves digging into the filesystem configuration and the filetab file.

First, I checked the Maximum mount count for my external drive.

sudo tune2fs -l /dev/sda1|grep Max

...which gave me a value of -1, meaning fsck is disabled on /dev/sda1. That number has to be a positive integer in order to run checks; if I want to check it every time, I run:

sudo tune2fs -c 1 /dev/sda1

If I wanted to check every other mount, I'd change that 1 to a 2. Or 3 for every third. You get the idea.

The second thing to check is the PASS count, which is in the fstab file. I need the UUID of my external drive;

sudo blkid|grep sda1

...which gives the long string of characters identifying the drive. I then grep in my fstab:

grep 7c275 /etc/fstab

...where the 7c275 was a hopefully distinct set of characters from the ID so I didn't type it all in while being too lazy to cut and paste. That gave me the result:

UUID=4ba44c85-659d-45fc-b36a-dad3e8e7c275 /mnt/mydrive ext4 defaults,noatime 0 2

...and the end of that line, the 2, is the part that gave the PASS value. According to this website, 0 means disabled (don't check), 1 means it has a higher priority and check first (usually reserved for /), and 2 means these partitions should be checked last.

At this point it seems my PASS value is 2 and the maximum check count is 1 for the external drive, and a /forcefsck is on the root filesystem. A reboot means that running tune2fs should show today's date as the most recent check upon restart...time for sudo shutdown -r!

Note: Seems no matter what option I try, my /dev/mmcblk0p2 is reporting its last fsck check, with tune2fs, was performed in the far past by several months. But when I check the logs with a grep of fsck, they show that a check was performed on that device at bootup. Not sure why it's not registering.

Note 2: Websites for reference information are here and here.

No comments:

Post a Comment