I'm currently improving my initramfs.
I wanted to create a function which checks all the available filesystems if possible.
I want it to be fs agnostic.
Looks like the fsck -command does not have an option to only check filesystems (what? why?!?).
Any clever ways to do check on all available filesystems (no fstab requirement) for errors (but not repair) in busybox shell inside initramfs?
This of course needs external fs utilities, but that's not my concern, I can add more utils. I can even load another cpio which contains them If needed.
Filesystem-specific options
Options that are not understood by fsck are passed to the filesystem-specific checker. These arguments must not take arguments, as there is no way for fsck to be able to properly guess which options take arguments and which don't.
Options and arguments which follow the -- are treated as filesystem-specific options to be passed to the filesystem-specific checker.
Please note that fsck is not designed to pass arbitrarily complicated options to filesystem-specific checkers. If you're doing something complicated, execute the filesystem-specific checker directly. If you pass fsck some horribly complicated options and arguments, and it doesn't do what you expect, you're almost certainly doing something you shouldn't be doing with fsck. Options to different filesystem-specific fsck's are not standardized. If in doubt, please consult the man pages of the filesystem-specific checker. Although not guaranteed, the following options are supported by most filesystem checkers:
-a Automatically repair the filesystem without any questions (use this option with caution). Note that e2fsck supports -a for backward compatibility only. This option is mapped to e2fsck's -p option that is safe to use, unlike the -a option that some filesystem checkers support.
-n For some filesystem-specific checkers, the -n option causes the fs-specific fsck to avoid attempting to repair any problems, but report such problems to stdout. However, this is not true for all filesystem-specific checkers. In particular, fsck.reiserfs does not report any corruption if given this option. fsck.minix does not support the -n option at all.
-r Interactively repair the filesystem (ask for confirmations). Note: It is generally a bad idea to use this option if multiple fsck's are being run in parallel. Also, note that this is e2fsck's default behavior; it supports this option for backward compatibility reasons only.
-y For some filesystem-specific checkers, the -y option causes the fs-specific fsck to always attempt to fix any detected filesystem corruption automatically. Sometimes an expert can do better driving the fsck manually. Note that not all filesystem-specific checkers implement this option. In particular, fsck.minix and fsck.cramfs do not support the -y option.
ie. if it's an ext-filesystem it effectively uses e2fsck which support the -n option.
If you call raw fsck (i.e. not qualified like fsck.ext5), the busybox implementation just passes the command line to whichever fsck is needed; I'm not sure if the choice is driven by probing the fs or by reading /etc/fstab. If it's the latter, you can find the necessary info from blkid instead.
As you mentioned, you need to include whichever fsck.foo's you need in your initramfs; busybox's one doesn't actually do anything, it just passes the commands along.
fsck's normally respond to "-p" - i.e. "preen" as forced check-and-repair.
fsck "-n" is non-interactive check. opening the FS read-only, which I think is what you want. ext4 and fat both support it; f2fs doesn't mention it; I'll give it a try in a moment (needs a reboot).
I think it's up the the individual fsck.foo what they implement, but equally, I think they try to be consistent for the easy ones.
<edit>
Busybox's fsck needs a valid /etc/fstab. Even if you tell it the fs type with -t ! so you need to either know, or read blkid output, extract fstypes, and build the appropriate command.
f2fs is a pain in the but. Rejects "-n", and uses "--dry-run" instead.
</edit>
blkid | awk '
{
dev=substr($1,0,length($1)-1)
match($0,/ UUID=\"[-0-9a-fA-F]+\"/)
if (RLENGTH > 0) {
uuid=substr($0,RSTART+7,RLENGTH-8)
if (!seen[uuid]++) {
match($0,/ TYPE=\"[-_a-zA-Z0-9]+\"/)
if (RLENGTH > 0) print dev,uuid,substr($0,RSTART+7,RLENGTH-8)
}
}
}
'
This outputs path, uuid and type and drops duplicate UUIDs and entries which don't have UUID and/or TYPE. I can then loop trough the output line by line in sh and run appropriate fsck commands.
If only busybox blkid gave us the option to format its output...