How to force fsck at boot?

I am using LEDE 17.01.4 on Raspberry Pi 3B.

To protect against SD card corruption at powerloss, i want to force a filesystem check of the root partition /dev/memblk0p2 which is an ext4 partition as provided by the LEDE image.

I tried all options i could find on the internet, but either they are outdated or not relevant to LEDE:

  1. tune2fs -c 1 /dev/memblk0p2
    This sets the partition to be checked on boot; but i get only a message i should check the filesystem.the check is not executed

  2. touch /forcefsck
    does nothing aside from creating /forcefsck

  3. appending the cmdline.txt with fsck.mode=force and fsck.repair=yes
    does nothing

  4. shutdown -rF
    not available on LEDE

  5. modifying /etc/options/fstab
    file not available on LEDE

So can anyone please tell me the proper method to enable fsck at boot in a recent LEDE/OpenWRT?

fsck is not included as part of the ash shell.

So you are saying there is no method to fsck at boot?

How can a headless router without changeable media then be freed from a broken filesystem? This would then always mean reinstallation once occured?

Try...

opkg install e2fsprogs

e2fsprogs is of course installed and fsck.ext4 and e2fsck are available.

But it is impossible to check the root-fs while it is mounted; this needs to be done before mount by the kernel as i understand. And i am not getting this to execute at boot.

You may have to unmount manually...

umount -l partition name

e2fsck -y -v -f partition name

It is the root partition. I doubt i can unmount it. And even if i could, i would not have a working system anymore...

Put it in read-only mode.

1 Like

Alright, remounting in read-only mode did the trick; thank you!
Somehow i thought of ro-mode as not useful and write-protected also for fsck. But fsck and tune2fs can modify the filesystem in ro-mode.

Now i will combine it with another tip to put the check before rw-remount in:
/lib/preinit/80_mount_root

or i will wait for boot-completion and do the check with a few remounts.

So while this is possible to have the running system fsck'ed i wonder why the process of checking and fixing is not supported by LEDE/OpenWRT on its own? Sudden powerloss accidents do happen from time to time ...

2 Likes

Hi connor2,

I am looking a way to check rootfs before it is mounted, could you give me some clues or give me some instructions to do that.

Thanks

Tuan Nguyen

I'm not sure which exact command @conner2 used but I had success on the Raspberry Pi (running 17.01 based system) by modifying the /lib/preinit/80_mount_root file adding this new line (you need to have the e2fsprogs package installed):
/usr/sbin/e2fsck -y -v -f /dev/mmcblk0p2
before this existing line:
[ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_mount_root

Also I tested the in-built "check filesystem" option under the menu System->Mount Points but it didn't appear to work and in most cases gave a 'Bad Gateway' error after which the only recovery path was a reboot/power cycle.

Anyway it would be great if there was working support for filesystem checks in OpenWRT/LEDE as some storage systems and set ups aren't quite so reliable.

1 Like

EXT4 is a journaling filesystem, after a power outage the journal will be replayed during the next mount.

On desktop systems, it behaves like this...on OpenWRT/LEDE it does NOT!

As stated in the initial post, no method whatsoever will enable this at boot for the root partition.

My solution is to drive the system in read-only mode for all filesystems that reside on the sd-card. I additionally mount an USB-stick with ext4 to write stuff. There it behaves like you would expect and properly recovers from power failures and such incidents.

When a file-system has gone to read-only state, you can fsck and repair it from within a running running system...even if it is the root partition. And then you can force it to stay read-only as i have explained above. Or you need to take out the sd-card, check and repair it on a desktop system and then prevent it from going to write-mode ever again if you want to protect it.

1 Like

add fsck in /lib/preinit/80_mount_init to check the disk every time it boots
the modified file looks like

#!/bin/sh
# Copyright (C) 2006 OpenWrt.org
# Copyright (C) 2010 Vertical Communications

do_mount_root() {
#code for checking disks
        if [ -x '/usr/sbin/fsck.ext4' ]; then
                if [ -e '/dev/sda1' ]; then
                        echo "Checking disk /dev/sda1...."
                        /usr/sbin/fsck.ext4 -y /dev/sda1
                fi
                if [ -e '/dev/sda2' ]; then
                        echo "Checking disk /dev/sds2..."
                        /usr/sbin/fsck.ext4 -y /dev/sda2
                fi
        fi

        mount_root
        boot_run_hook preinit_mount_root
        [ -f /sysupgrade.tgz ] && {
                echo "- config restore -"
                cd /
                tar xzf /sysupgrade.tgz
        }
}

[ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_mount_root

2 Likes

Just a side note here:

According to https://openwrt.org/docs/techref/block_mount, through configuring

option 'enabled_fsck' '1'

shall work.

1 Like

can you translate that to simpler terms?. The reference link is way over my head. I have a USB3 external drive and would like to have the fsck run on boot. Where exactly is this line placed (in what config file)? Thanks.

You can also configure these things on the web GUI, under the "Mount Points" menu.

Oh, wow, that was easy. Under the "System", "Mount Points" following down the list of topics there is a section "Mount Points" with the USB drive is in the list and is checked if it is enabled. If you click the "Edit" option, then on the "Advanced settings" is a check box for "Run system file check" (on boot). Excellent! Thanks!!

1 Like

Warning:

After I did that, my router is in readonly mode. And I have no way to put it back.

How to know it's getting executed?
I added prints with "logger "Checking disk /dev/sda1...."; I don't see these in the logread output.