19

How to set systemd for automatically force fsck disks after crash (hard poweroff)?

When I used sysvinit (in Arch Linux) as /sbin/init I used the hack: in the rc.local I create /forcefsck file; in the rc.local.shutdown I remove it. At boot-up rc.sysinit enable force fsck if /forcefsck exists.

How to do same in systemd? Maybe it has built-in facilities for automatic fsck after crush?

1
22

You can force fsck at boot time by passing fsck.mode=force (auto is default and skip can be used to skip checking at all) as a kernel command line parameter (as of systemd v. 213, there's also a second parameter: fsck.repair - to control how fsck shall deal with unclean file systems at boot; possible values are: preen to fix what can be safely fixed, yes to answer yes to all questions and no is default).


Note that systemd-fsck does not know any details about specific filesystems, and simply executes file system checkers specific to each filesystem type (/sbin/fsck.*).
Now, if your filesystem is xfs or btrfs it will execute /sbin/fsck.xfs or /sbin/fsck.btrfs respectively. If that does not seem to work maybe you should check the manual page for fsck.xfs or fsck.btrfs respectively and examine the contents of the said files in /sbin.

2
  • 2
    For example with Grub: edit /etc/default/grub, then append fsck.mode=force to the value of GRUB_CMDLINE_LINUX_DEFAULT. Then run update-grub and reboot. – Yvan Sep 6 '16 at 8:38
  • 3
    doesn't work for xfs btw. but hey. this is the future. – Florian Heigl Oct 13 '16 at 23:14
0

The only way to check BTRFS system is to use its own tool btrfs check, you must have the root volume unmounted therefore the only option is to really boot from a livecd.

Any advice that ultimately leads to calling fsck.btrfs is wrong, this is just a stub that prints out a message and does nothing. This includes answers with fsck.mode kernel command line options or .forcefsck files as well.

[root@nuc ~]# cat /usr/sbin/fsck.btrfs
#!/usr/bin/sh -f
AUTO=false
while getopts ":aApy" c
do
    case $c in
    a|A|p|y)    AUTO=true;;
    esac
done
shift $(($OPTIND - 1))
eval DEV=\${$#}
if [ ! -e $DEV ]; then
    echo "$0: $DEV does not exist"
    exit 8
fi
if ! $AUTO; then
    echo "If you wish to check the consistency of a BTRFS filesystem or"
    echo "repair a damaged filesystem, see btrfs(8) subcommand 'check'."
fi
exit 0

However if you are just unsure if everything is okay after poweroutage or something like that, btrfs check can perform a readonly check on a mounted filesystem:

[root@nuc ~]# btrfs check --readonly --force /dev/sda5
Opening filesystem to check...
WARNING: filesystem mounted, continuing because of --force
Checking filesystem on /dev/sda5
UUID: 8c44de9c-c91b-4ac4-857b-da191dc62274
[1/7] checking root items
[2/7] checking extents
[3/7] checking free space cache
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data)
[6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS)
found 3628683264 bytes used, no error found
total csum bytes: 3093864
total tree bytes: 136937472
total fs tree bytes: 126074880
total extent tree bytes: 6455296
btree space waste bytes: 23047273
file data blocks allocated: 5676253184
 referenced 4705763328

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy