Score:5

How to force fsck on reboot for Ubuntu 20.04

de flag

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?

vanadium avatar
cn flag
@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?
Score:4
cn flag

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.

gog avatar
cn flag
gog
`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)
Freedo avatar
in flag
Why do you say grub entry is better than the other option?
vanadium avatar
cn flag
@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.
blackjack75 avatar
in flag
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.
Валерий Заподовников avatar
-c 1 is "invalid" and modern tune2fs 1.47.0 complains about it. LOL. Just set it to 2 and reboot two times.
Score:2
us flag

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 avatar
cn flag
I am not sure whether the old system `sudo touch /forcefsck` still works since `systemd` does the initialisation.
vanadium avatar
cn flag
I am even not sure anymore whether file system check is still automatic in Ubuntu! `sudo tune2fs -l /dev/nvme0n1p2 | grep checked` for me gives `Last checked: Sat Oct 31 13:03:57 2020`! `fstab` still contains `1` as the last field.
Kunal Shah avatar
us flag
@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 avatar
us flag
@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
vanadium avatar
cn flag
I am aware of the meaning of 1 in fstab. I should maybe have expressed more precisely. The partition is being checked, however it is never *deep* checked (i.e. explicitly scanning the file structure rather than just checking whether the journal reports "OK")! In the old days, a deep check would be forced every every 30 mounts. Currently, a deep check is never imposed and will happen only when the journal is unclean - but then it may already be too late. tunefs will only register a deep check.
midenok avatar
cn flag
@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.
vanadium avatar
cn flag
@midenok I posted an answer halve an hour later (see above), so I did follow your advise.
vanadium avatar
cn flag
@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.
Валерий Заподовников avatar
You cannot use -c 1 now. Use -c 2 and reboot two times.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.