17

I'm working with a fanless PC (hundreds of them, in fact) that has debian 6 and 3 partitions( FAT and 2x ext2). The system doesn't have a power button as such so most people tend to yank the plug to 'shut it down' rather than init 0 (or equivalent). As a result the filesystem(s) build up errors pretty rapidly.

I've tried using 'shutdown -rF' to force fsck but this doesn't seem to be working. I'm wondering if there is some way to tell the system to check each mount point / FS before they are mounted.

I've tried setting the fsck param in /etc/fstab. This typically gives me a 'Errors found. Run fsck manually' message.

Are there other options to try?

ethrbunny
  • 743
  • 3
  • 9
  • 24

3 Answers3

17

In /etc/init.d/checkfs.sh is the line if [ -f /forcefsck ] || grep -s -w -i "forcefsck" /proc/cmdline, so providing forcefsck on the kernel command line or generating a /forcefsck file on shutdown should cause an fsck on the next reboot.

To prevent manual fsck runs, ask fsck to try to automatically fix errors with the -y option by uncommenting and changing no to yes in the following /etc/default/rcS entry, after the edit it should look like:

# automatically repair filesystems with inconsistencies during boot
FSCKFIX=yes

One option (forcefsck or FSCKFIX) does not imply the other.

finite graygreen
  • 697
  • 5
  • 11
  • Will this handle the 'Run fsck manually' condition? – ethrbunny Apr 09 '14 at 17:58
  • Indeed, I read too fast, updated my answer. – finite graygreen Apr 09 '14 at 18:11
  • I have set both FSCKFIX=yes and the value in `checkfs.sh`. In neither case did fsck run when I rebooted (via init 6). I must be missing something here. – ethrbunny Apr 09 '14 at 18:34
  • Don't change anything in `checkfs.sh`, only edit /etc/defaults/rcS and run `touch /forcefsck` before reboot. The last command needs to be run before *every* restart or just after you booted but the fsck init script is done. – finite graygreen Apr 09 '14 at 18:42
  • Hmm.. no luck. I still see the 'mounting unchecked filesystem' message followed by lots of 'delete inode referenced' errors. So far changing /etc/fstab has been the only thing has had any effect. Not sure what I'm doing such that your suggestions aren't working. – ethrbunny Apr 09 '14 at 18:54
  • Interesting: I set the bit in fstab to 2 and rebooted. This time all the interactive fsck questions got a 'yes'. I wonder if it's a combination of the FSCKFIX=yes and the fstab entry that would work. – ethrbunny Apr 09 '14 at 19:03
  • See `man 5 fstab`, the "second number" means `The sixth field: is used by the fsck(8) program to determine the order in which filesystem checks are done`, 0 means no fsck, 1 means first (usually for the root fs) and 2 means after that. – finite graygreen Apr 10 '14 at 10:29
10

Add the fsck.mode=force Kernel parameter on your bootloader. Append this option to the GRUB_CMDLINE_LINUX_DEFAULT= variable inside /etc/default/grub. As root, generate a new grub configuration file:

[root@host]# grub-mkconfig -o /boot/grub/grub.cfg

This parameter have the same effect as creating the forcefsck file inside the root of the mount point.

The solution of using the checkfs.sh script will better fit on Debian, but should not work on other distros

  • ~~Instead of changing every entry, there should be a GRUB entry for this like the Advanced/Recover mode entry. ~~ I take back what I say, hack, grub entries are useless in headless mode ... – Ben Oct 18 '22 at 03:37
8

I am editing my answer:

The command is:

sudo tune2fs -c 1 /dev/sdX

according to manpages -c argument for tune2fs counts number of instances of mounts for a partition. Hence, 1 forces to check the fs after every mounting instance.

(http://man7.org/linux/man-pages/man8/tune2fs.8.html)

xitij
  • 381
  • 3
  • 6
  • 2
    Only a command is not enough in most cases, write at least a minimal explanation, too. – peterh Mar 03 '15 at 08:30
  • This is what I was looking for, since I have to fix my corrupted fylesystem (Ubuntu 16 LTS) very often. I put a new Kingston SSD one year ago, so I don't know exactly why this issue is so recurrent. – Jose4Linux Jun 14 '20 at 22:49
  • Does this also fixes any issues without user input? like with -y ? – Freedo May 18 '22 at 08:42