OpenWRT fsck at boot

Hey guys

I was creating an account for asking this question:

How can I make my RPi 3 B+ router do automatically fsck.ext4 /dev/mmcblk0p2 (root partition) after sudden powerloss?

I was trying many things like using „block-mount“ package recommended by the official documentation
fstab
which results in an ro-/dev/root on /rom garbage.

I also tryed to use /etc/rc.local startup file and crontab for executing a custom script in /etc/init.d/ (executable) without success.

Of course editing /boot/cmdline.txt by adding „fsck.mode=force“ and „fsck.repair=yes“

Has anybody an idea about how to make openwrt an automatic fsck on powerloss instead of just leaving the filesystem in read only state?

Thanks

Always do fsck, before you (re)mount ?

I don‘t want to do this by myself. It is in case of an emergency like I mentioned before; powerloss etc

What difference would it make, if scripted ?

1 Like

What do you mean? A scripted powerloss? :smiley:

Always fsck during boot, no matter the reason for the (re)boot.

Ok, but how? I tryed everything

Don't mount it automatically.

Then add fsck and mount to rc.local.

But /etc/rc.local is located within /. How to execute a script that cannot be accessed?

Good point.

Seems I missed that tiny detail, sorry.

Try booting the root in ro mode (just att ro to the kernel boot string), then it should be accessible, and fsck:able.

But you really go to have bad luck with your storage, I have the 1st gen RPi, running the same SD card since I set it up, when the RPi came out.
It's a regular Linux OS, not Openwrt.
Never ever had any issues with it.

1 Like

Long story short: it is impossible. This holds true for every device, which boots from a sdcard. Why?

On a classic x86 device Linux is booted with an initrd. After coming up this system has no partition mounted. So it can fsck and repair any filesystem. After that step the partitions are mounted according to /etc/fstab.

Pi-like devices do not use an initrd, but mount the root partition read-only in order to start the boot process. Just google for "e2fsck on a read-only partition" and you will get many explainations, why this is dangerous and an absolute NO-GO.

If there are additional partitions, it is possible to apply a fsck on these partitions as long as they are not mounted. For this task I've created an init-script, which is called as soon as possible. If the fsck is successfull, the checked partitions are mounted too.

Well how do you achive this in detail?
You boot / fs in read only or how are you able to read your init-script?
Why is openwrt not reading the cmdline.txt on the boot-partition anyway? This should do the trick because the kernel is loaded into ram and / is not mounted jet.

In order to load the kernel and execute other programs/scripts you must mount the root partition, at least read-only. Therefore: no chance to fsck+repair the root partition.

The maximum you can do is calling e2fsck -n $root_partition

The option -n means: check, but do not repair/change the filesystem. When e2fsck has finished, the exit code tells you, whether the filesystem is clean or not.

If the filesystem is not clean, there is no other chance than pulling the sdcard off the device, insert it in a card-reader and fsck+repair it on another device (PC or laptop).

Ok, I see..
Is it possible then to mount / fs ro and create a partition for writing persistent stuff in /etc and so on?
I was curios because @connor2 posted a similar config here. At least it sounds like that.

try this method,
edit /etc/init.d/boot

grep -q "ext4.*ro" /proc/mounts || /usr/bin/mount -o remount,ro /
/usr/sbin/e2fsck -v -f -y /dev/mmcblk0p2 > /tmp/e2fsck_boot.txt
grep -q "ext4.*rw" /proc/mounts || /usr/bin/mount -o remount,rw /
/bin/config_generate

add fsck command before /bin/config_generate, like above example.
i am using this way for a long time and its working fine.

example e2fsck_boot.txt

Pass 1: Checking inodes, blocks, and sizes
Inode 7, i_size is 130076672, should be 134316032.  Fix? yes

Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information

rootfs: ***** FILE SYSTEM WAS MODIFIED *****
rootfs: ***** REBOOT SYSTEM *****

        7052 inodes used (0.18%, out of 3899392)
           1 non-contiguous file (0.0%)
           0 non-contiguous directories (0.0%)
             # of inodes with ind/dind/tind blocks: 0/0/0
             Extent depth histogram: 6517
      308682 blocks used (1.98%, out of 15597568)
           0 bad blocks
           0 large files

        6027 regular files
         509 directories
           0 character device files
           0 block device files
           0 fifos
           0 links
         507 symbolic links (504 fast symbolic links)
           0 sockets
------------
        7043 files

Unfortunatelly this doesn‘t work either.
It still has mounted / as ro and last fsck was the one I ran manually as shown by tune2fs -l.

TLDR, but it won't remount as rw unless you tell it to ?
fsck or not.

Indeed that‘s a point, one could add && reboot or something like that. But fsck doesn‘t run at all :confused:

(Doen‘t make sence to mount an unclean fs to rw)

@connor2 proposed an e2fsck on the read-only mounted root partition in /lib/preinit/80_mount_root. For me that's an absolutely NO-GO. Google for "e2fsck on read-only mounted partition" and you will know why.

The mount point doesn't matter, what matters for an e2fsck is: the partition/filesystem is not mounted. After the fsck+repair you can mount it where ever you like.

I understoot that this practise seems to be a bad habit. Hopefully connor and I will get some christmas presents despite that :wink:
Anyway whats the matter? It is just a router with no important data on its machine. So if the fs gets corrupted I‘ll install a new OpenWrt from my backup.

I‘ll try to add new partitions for wrtiting stuff so that / can stay ro :slight_smile: