I am trying to deal with a very rare corner case where my system has a minor corruption in the ext4 filesystem. This trigger systemd's emergency.service
and I am trying to alter the behavior of it to automatically fix the fs.
Anyhow, is there any way to slightly alter the partition/filesystem to induce this state with minimal chance of dataloss? I just need fsck
to return that something is wrong.
If emergency.service runs, the initrd switch root or the fsck has already been attempted and failed. systemd-fsck-root.service can be told to always repair (fsck -y) with the kernel command line parameter fsck.repair=yes
fsck will be skipped for any file system with passno value of 0 in /etc/fstab (6th field). Possibly a bad idea in a system with a history of corruption, but if the file system mounts allows continued operation.
File systems can be made not required for local-fs.target with a nofail option in fstab. However this does not work for root fs. Consider creating a new file system at the mount point you suspect contains problem files, for example /var/lib/whatever. With nofail option, even if it fails to mount, will not be dropped into an emergency shell.
Backups are the last line of defense to avoid losing important data. Do not rely on repair to always fix things, even on mature file systems.
Some corrupted file systems exist in the e2fsprogs test suite. Tricky to use for real, espcially as a root fs. Image your block device with it, extend it larger, and restore your data from backup.