18

I am running Ubuntu 20.04 on an embedded device. I will like to ask how to force automatic fsck on bootup? In older Ubuntu versions, this can be achieved by editing the /etc/default/rcS and setting FSCKFIX=yes. However, I read that in newer Ubuntu, this file is not used anymore. So what will be the way to enable automatic fsck on bootup for newer Ubuntu?

ablan
  • 195
  • @guiverc question is about how to initiate a fsck during boot, like we could in earlier days using sudo touch /forcefsck. To my consternation, I see today: sudo tune2fs -l /dev/nvme0n1p2 | grep checked - Last checked: Sat Oct 31 13:03:57 2020. Does that indicate that the system file system is not anymore automatically checked nowadays? – vanadium Jul 19 '21 at 11:43

3 Answers3

21

The old convenient trick of creating a file /forcefsck to force a file check on reboot does not work anymore since systemd took over initialisation.

Change "Maximum number of mounts" (only ext file systems)

The quickest way, probably, is to temporarily change your Maximum mount count to 1. That will cause the kernel and e2fsck to check the file system on the next reboot. However, this only works with ext file systems.

First check your current setting in case you want to restore to default later:

sudo tune2fs -l /dev/nvme0n1p2 | grep 'Maximum mount'

Substitute /dev/nvme0n1p2 by the device name of your system partition. Chances are this is set to -1 nowadays, disabling check based on the number of times the volume has been mounted.

Adjust the setting to 1 with the command:

sudo tune2fs -c 1 /dev/nvme0n1p2

Now reboot - the volume should be checked. After reboot, you should reset the value to what it was before in order to avoid the partition being checked everytime.

Change kernel parameter

Another way is to pass kernel parameters during boot that control the systemd services for file system check. fsck.mode=force will force a file check.

To add a kernel parameter for a single time, boot to the Grub menu, highlight the entry and hit e. Move to the line starting with linux, hit End to move to the last line, add a space and the kernel parameter. Hit Ctrl+x to close and continue booting.

vanadium
  • 88,010
  • 1
    fsck.mode=force is the best solution, and the most similar to the /forcefsck old behavior. The best would be to create a new dedicated GRUB entry for that IMHO. (+1) – gog May 03 '22 at 11:35
  • Why do you say grub entry is better than the other option? – Freedo May 18 '22 at 09:12
  • @Freedo I am not saying that as far as I see. I am providing two options, one specifically for ext4 (there may be similar options for e.g. btrfs or not), and the grub way, which does not depend on the specific file system. – vanadium May 18 '22 at 16:26
  • 2
    Might seem obvious to some but will using this solution will fsck auto repair errors or just wait for user to enter a 'y' in case of errors ? I would like to autofix to make sure I can still access it remotely if this happens. – blackjack75 Mar 21 '23 at 14:49
  • -c 1 is "invalid" and modern tune2fs 1.47.0 complains about it. LOL. Just set it to 2 and reboot two times. – Валерий Заподовников Jun 05 '23 at 03:10
5

If you want to force fsck with each reboot, there are few steps you need to follow.

  1. use blkid to identify the uuid for the partition.
amarcus@amarcus-desktop:~$ blkid
/dev/mapper/vgubuntu-swap_1: UUID="d24b0766-c9be-49ef-9022-8ccae4f79801" TYPE="swap"
/dev/mapper/vgubuntu-root: UUID="d414c4f9-da0d-42bf-8290-4bcb55b8d984" BLOCK_SIZE="4096" TYPE="ext4"
amarcus@amarcus-desktop:~$

  1. Use uuid or mount point to locate the partition in /etc/fstab
amarcus@amarcus-desktop:~$ cat /etc/fstab 
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/vgubuntu-root /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/nvme0n1p1 during installation
UUID=0AE0-795B  /boot/efi       vfat    umask=0077      0       1
/dev/mapper/vgubuntu-swap_1 none            swap    sw              0       0

UUID=D41C-2F17 /mnt/WDElements auto umask=0022,gid=1000,uid=1000,x-gvfs-show 0 0

The last column that is a column 6, aka fsck PASS column is used by fsck to determine whether fsck should check filesystem before it is mounted and in which order given partitions in /etc/fstab should be checked

For root partitions, make sure that entry is set to 1

  1. Finally, set the mount counter for that partition to 1.
root@amarcus-desktop:~# tune2fs -c 1 /dev/mapper/vgubuntu-root

Explanation:

Basically, in step 1 you are identifying which partition you want to check at boot.

In step 2, you are making sure that it takes higher priory. It's useful if you are checking more than one partitions. It decide which should be taken up first, then second and so on.

In step 3, you are saying after how many mounts the partition should be checked. The argument 1 specifies that after one mount the partition should be checked. So basically it checks after each mount, i.e. after every restart.

  • @vanadium you are right. I just checked in my new 21.04 system and sudo touch /forcefsck is not working. Good news is that tune2fs -c 1 /dev/mapper/vgubuntu-root works. It checks file system after each reboot in my ubuntu 21.04 – Kunal Shah Jul 20 '21 at 06:59
  • @vanadium - about your message on

    sudo tune2fs -l /dev/nvme0n1p2 | grep checked

    In your fstab when root partition is 1 in last column does not mean that it will check the partition after 1 mount. That number in fstab is the priority in which the partition should be checked. Making sure it is 1 for root partition means whenever file system is checked, root partition will be checked "first".

    In my answer - in step 3, when you run tune2fs -c 1 /dev/mapper/vgubuntu-root that is where you are specifying file system check after each 1 mount.

    Hope this helps

    – Kunal Shah Jul 20 '21 at 13:27
  • @vanadium If you are not sure you just try it and post the right answer as nobody wants to know your doubts. Good thing is testing this takes no more than couple of minutes. Bad thing is now your doubts are read forever by infinite number of people. – midenok Sep 08 '22 at 20:00
  • @midenok I posted an answer halve an hour later (see above), so I did follow your advise. – vanadium Sep 08 '22 at 20:21
  • @midenok Also note that comments are only intended to improve on a question or post, and thus are temporary. They actually may disappear any time - I deleted mine here, and I would suggest you delete your comment as well. – vanadium Sep 08 '22 at 20:25
  • You cannot use -c 1 now. Use -c 2 and reboot two times. – Валерий Заподовников Jun 05 '23 at 03:37
3

Up to 20.04 under root privileges:

touch /forcefsck
reboot

You don't have to remove /forcefsck as the system removes it.

After 20.04 (thanks to systemd which never makes your life easier):

Add fsck.mode=force and fsck.repair=yes to kernel boot parameter either as new grub menu item or by editing kernel string at booting.

midenok
  • 792
  • 1
  • 8
  • 13
  • 1
    Another answer states that it doesn't work anymore: https://askubuntu.com/questions/1352774/how-to-force-fsck-on-reboot-for-ubuntu-20-04/1352782#1352782 – A.L Oct 04 '22 at 21:28
  • I don't tell anything that didn't work for me. That would be stupid: push dead information back and forth. @a-l Did you check that it doesn't work or you just push information back and forth? – midenok Oct 27 '22 at 16:45
  • 3
    yup, doesn't work anymore on ubuntu (ubuntu 22.04.1 LTS). No need to call anyone stupid. – Tiago Dec 19 '22 at 12:23
  • Can confirm that it works on Ubuntu 20.04. And it's a much simpler solution than the nightmare of confusion mentioned in the accepted answer.that said, maybe it won't work when I upgrade to 22.04. there should be some simple way to do this though! – gannex Jul 27 '23 at 17:42
  • @midenok I think this solution is valuable since it's so much simpler, but maybe you should consider adding that it only works for ubuntu <20.04 – gannex Jul 27 '23 at 17:43