1

I posted this question on the Raspberry Pi forum but got no activity. If it is a dumb question or something else, please say so...

While not often, I do experience power outages which stop one of my Pis and then usually results in the USB stick having the "dirty bit" set.

I know how to fix this manually:

sudo dmesg |grep sda1 ==> this finds references to sda1 in the message log.

If I see that it has reported a dirty bit for sda1 then I do the following:

  1. sudo umount /boot
  2. sudo fsck -V /dev/sda1 ==> answer questions based on the following output:

    fsck.fat 3.0.24 (2013-11-23)
    0x25: Dirty bit is set. Fs was not properly unmounted and some data may be corrupt.
    1) Remove dirty bit
    2) No action
    

    I reply this: ? 1
    More output:

    Starting check/repair pass.
    Starting verification pass.
    Leaving filesystem unchanged.
    /dev/sda1: nn files, mmm/ooo clusters
    
  3. sudo fsck -a /dev/sda1

  4. mount /boot

However, I would like to automate this in a script which could be run manually or during boot up. Can anyone assist or point me to a good reference? Obviously, I may also want to do this for the SD card OS partition.

2 Answers 2

2

You don't really need a script to detect a dirty bit. Just do the check every time: fsck is smart enough not to touch a file system if there's nothing to fix.

Native Linux file systems have a built-in option telling the system how often they should be checked. If you want your root FS to be checked on every tenth reboot, run

tune2fs -c 10 /dev/mmcblk0p2

Another option to consider is journaling, which will eliminate the need for frequent fsck checks, at the expense of increased wear of your SD card.

0

Ran into the same problem, and your question contained the answer I needed!

Since I now need the same script as you, I thought of paying you back :-) (hopefully you are still interest after over a year later)

I'm not a pro, so this might be highly unefficient, but it seems to work for me:

1) Save your first command's output to a temp file

sudo dmesg |grep sda1 >> /home/pi/temp_file

2) Check if there's the "dirty bit set" message in the temp file

if grep "Filesystem has been set read-only" /home/pi/temp_file

3) If it's the case, unmount (same command), run fsck with the automatic fixing without prompt option (-a)

sudo umount /dev/sda1
sudo fsck.vfat -a /dev/sda1
sudo reboot

4) You should delete the temp file before rebooting and before exiting the script. You can also add logging for good measure.

Full example:

#!/bin/bash

date >> /home/pi/test_sh.log
sudo dmesg |grep sda1 >> /home/pi/temp_file
if grep "Filesystem has been set read-only" /home/pi/temp_file
then
echo "Dirty bit set!" >> /home/pi/test_sh.log
rm /home/pi/temp_file
sudo umount /dev/sda1
sudo fsck.vfat -a /dev/sda1
sudo reboot
else
echo "Everything ok" >> /home/pi/test_sh.log
rm /home/pi/temp_file
fi
exit

(Note: I have this running once a day and the log purged once a week. It also runs on boot so just rebooting triggers the script. My SO has the habit of rebooting the RPi when it acts up :-P)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.