Sometimes you may get an error while running the df command saying that tat the /etc/mtab file is either corrupt or empty(removed). It may be a case that someone accidentally edited the /etc/mtab file and has incorrect data. Well, this post will help you restore your correct mtab file.
What is the use of /etc/mtab file
Before we begin let’s see what an mtab file is. /etc/mtab is the file that maintains a list of currently mounted filesystems. So when you run a “df” command, it referes to this file to generate an output to you. The term “mtab” referes to “Table of Mounted Filesystems”. The mtab file has a soft link to the file /proc/self/mounts.
# ls -lrt /etc/mtab lrwxrwxrwx. 1 root root 17 Sep 29 2014 /etc/mtab -> /proc/self/mounts
There is another file called “/proc/mounts” which is also soft linked to the file “/proc/self/mounts”.
# ls -lrt /proc/mounts lrwxrwxrwx. 1 root root 11 Jun 11 11:49 /proc/mounts -> self/mounts
/proc/mounts is the kernel’s view of the mount points. So its kind of stored in the realtime memory of the system. For the man page of mount:
# man mount ... It is possible that files /etc/mtab and /proc/mounts don’t match. The first file is based only on the mount command options, but the content of the second file also depends on the kernel and oth- ers settings (e.g. remote NFS server. In particular case the mount command may reports unreliable information about an NFS mount point and the /proc/mounts file usually contains more reliable information.)
How to recover a corrupt or removed /etc/mtab file
1. In order to recreate /etc/mtab file, we simply have to copy the /proc/mounts contents to /etc/mtab file. In other words, copy the kernel’s mount table to the mount table file with:
# cp /proc/mounts /etc/mtab
/proc/mounts is the kernel’s view of all the filesystems whereas the file /etc/mtab is the configuration file for the currently mounted filesystems. So /proc/mounts has a more accurate view of all the filesystems that are currently mounted. It also has the complete set of options that were used while mounting these filesystems.
Accidental removal of soft link to /proc/self/mounts
It may occur that, the soft link to the /etc/mtab file might get removed accidently, causing df to not work. It may also severely impact the server by making it un-bootable. To fix the issue, you can recreate the symlink using:
# rm -f /etc/mtab # ln -s /proc/self/mounts /etc/mtab