Understanding “fsck” in Linux

Importance of FSCK Utility

No one can predict when the system will get crash or your filesystem gets corrupt and if it happens then you may lose all of your valuable data from your hard drive. If you found that your filesystem creates such inconsistency then it is always run fsck to check the integrity, and this can be completed by using the special command called “fsck”(Filesystem consistency check). You can run this command manually or can start at the boot times.

You will need to run “fsck” on the following situation occurs

1.Taking a backup of your filesystem

2. Files on your system become corrupt

3. To do the consistency check

Ex:1 To run fsck on the filesystem

Syntax:

#fsck   <filesystem>    or   fsck   <mount point dir>

Note: To run fsck on a  filesystem, the filesystem should be in the unmounted state and inactive, You should never run fsck on the mounted partition doing so would corrupt the filesystem.

First check the filesystem is in mounted or in the unmounted state by using the following command

#df  -h

As you can see from the above output,/dev/sda2 is in the mounted state, now unmount this filesystem by using the following command

#umount  /dev/sda2
#df  -h

Now run the fsck on this filesystem for integrity check

#fsck  /dev/sda2

As you can see from the output, fsck hasn’t found any errors from the /dev/sda2 filesystem.

To Repair the Linux Filesystems errors automatically

When the filesystems have more than one errors, then for each and every scan fsck will ask the confirmation before it proceeds to repair all the errors, apply -y option with the fsck command to do the check and repair automatically.

#fsck  -y   /dev/sda2

Running fsck on the mounted partition:

If you run the fsck on the active partition  then  the file system will go to the  corrupted state,

Understanding fsck exit codes

While running the fsck, we may get some error codes, below are some of the important error codes we will get after the execution

0 =No errors

1 =File system error corrected

2 = System should be rebooted

4 =  File system error left uncorrected

16 = syntax error

32 = Checking cancelled by the user

To check the fsck error codes, run the following command after fsck,

#fsck  /dev/sda2

#echo $?

The above command will produce some error code after the execution of fsck command

As you can see from the above output echo $? command produced “0” error code which says there is no error found on the fsck scan.

Visit my youtube channel for more online tutorials 👇🏻👇🏻

To check the filesystem only for errors and don’t  repair

When you want to scan only for the errors  and you don’t want to repair, then  run the below command with -n option

#fsck  -n  /dev/sda2

The above command will scan only for the errors.

To run the fsck only on Unmounted partitions

When you are not sure about the mounted and unmounted partition details, run the below command, this will run fsck only on the unmounted partitions, when fsck detected any mounted partitions while running it will skip running on that partitions.

#fsck  -M
To run a fsck check on all the available partitions

To do a filesystem check on all partitions(including root partition), run the following command with -A option

#fsck  -A

A = Run fsck on all the available paritions

if you want to skip running fsck on the root(/) partition, then add the -R option with fsck as shown below,

#fsck -AR

The above command skips running fsck on the “/”(root) partition and it runs on all the remaining partitions.

 

I hope you have enjoyed this tutorial if so Kindly subscribe and share it with your friends.
                                          🙏🙏 Thank you 🙏🙏
#############################################################
For More Videos Subscribe My Youtube Channel  Linux Vasanth
If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

################################################################################

 

 

Understanding Mount and Unmount Concepts.

Mounting is attaching the filesystem to the directory structure in Unix and UNIX like operating systems.As all we know the filesystem is used to organize the data on a system or in storage media like USB, CD-ROM, DVD.In flavors of UNIX operating system, all the directory falls below the / (i.e root) directory.
If you want to access the data on a storage or partition devices, you must let the Operating system to know in the directory tree where to mount the device,For example  if you want to access the files from CDROM, the user should inform the OS to make the filesystem  on the CDROM display in some directory, by default /media  or /mnt Directory exists for this purpose.So to access the data user must have to attach the partition to some mount point directory.

Mount point is nothing but, its a directory where the filesystem gets attached or mounted, So all the directories are accessible through “/ “only. Manually users can create the mount point directory at any locations.
Mount: To make  the  filesystem visible to the users and the operating system
unmount: To safely detach the filesystem from its mount point directory tree.usually when we need to shrink a filesystem size or running the filesystem check(fsck) to check the integrity test on the partition it should be on inactive status.
So in order to access the data from the partition, the user must have to attach the filesystem to some mount point directory structure.Let us see how to mount the partition to the mount point directory,
Before you start mounting you should create a partition and format it with the preferred filesystem(i.e ext3,ext4).All we have to do is create an empty directory and then attach the filesystem to this mount point directory.
Syntax: To mount a filesystem to mount point directory
#mount  -t    <Device to be mount>    <mount point directory>

-t  –> To mention the filesystem type(without -t option it takes the default filesystem).

Ex:1 To mount a filesystem


I have a  partition on one of my disks with the size 1GB (/dev/sda3), I am going to use this partition for mounting..
Create a new mount point directory
#mkdir   /mydata

#mount  -t  ext4   /dev/sda3     /mydata

After mounting use the “df” command to check whether it is mounted properly on the mount point directory.
To check the currently mounted filesystem details run the  below command,
#df  -h

h –> human readble format(i.e display the size in (KB,MB,GB)

From the above output, we can see the details of  all the currently mounted filesystem,
Let me explain you the above output in detail(column-wise),
1.The mounted device
2.Total size of the partition
3.Used size of the partition
4.Avaiable size
5.used size in %
6.Where it is mounted (mount point directory).

Ex:2  How to unmount a mounted filesystem safely?


Unmounting is nothing but brining  the active file system in to inactive state, for this we have to use the command “umount” followed by the filesystem name or you can use the mount point directory to safely detach it from its mount point directory.
Syntx:
#umount    <Filesystem>

or

#umount  <mount point directory>
Always remeber before you decide to unmount a filesystem  the following things you need to check,
1.The filesystems is in active state or not(if the filesystem is in active state make it inactive)
2.How many users are accessing that parition (inform all the users regarding this activity)
3.Once you have done all these you can use the umount command to detach the filesystem from its mount point directory.
Check the currently mounted filesystem details
#df -h

The above output shows /dev/sda2 is in mounted state, let us unmount this
To unmount
#umount  /dev/sda2

or

#umount  /mydata

Check whether it is unmounted safely, To confirm that Run once again the “df” command
#df -h

From the above output, it is confirmed the partition /dev/sda2 is unmounted.

Ex:3 How to mount a CD-ROM manually 


As we all  know /dev is the directory which holds all the device files, device files for the CD-ROM will reside in /dev directory.
For the removable media, we have two mount point directory available by default in Linux /media or /mnt The, CD-ROM will be mounted on either  /media or /mnt.
Now if you want to access the files from the CD-ROM, first insert the CD in to the drive and mount the drive manually to /media mount point directory to access the files from the CDROM.Make sure /media or /mnt directory is available before you mount the drive if the directory is not available create a new one.
Syntax
#mount  -t  <device>  <mount point directory>
Before you mount the CDROM we need to check and find the CD/DVD drives available.To do that we have a command “wodim” with the    -devices option it will scan and give the output names found inside the /dev directory.
#wodim  --devices
Sample output: wodim:Overview of accessible drives

0 /dev="/dev/sr0"   rwrw-- :  CD/DVDW SH-5H9K
From the above output CD/DVD drives could access from the location /dev/sr0, Now mount the CDROM
#mount  -t  iso9660    /dev/sr0   /media
Note: ISO9660 is the standard filesystem for all CD-ROM, So here we inform the mount command to mount the iso9660 filesystem type on /dev/sr0(Removable device)

The above output showing some error /dev/sr0 is write-protected because you are trying to mount the CDROM with read and write access, Remember always you have to mount the CD/DVD devices with ro permission only.
#mount -t  iso9660  -o  ro   /dev/sr0   /media

After mounting you can access  all the files from the CD-ROM from the mount point directory /media
Run the “df -h” command to  check the currently mounted filesystem information
#df -h

Now to access the files from the CDROM we need to change our path to the  /media directory and list  all the files
#cd  /media
#ls

From the above output, it prints all the files from the CDROM.

To unmount the  Media


#umount  /media
#df -h

Follow the same steps if you want to mount manually the DVD/floppy devices.

Importance of /etc/mtab file


In Linux to  monitor and track of mounted filesystem  the first method is  loaded in kernel and deliver the information to the user via /proc/mounts file, and the second method is with the file  /etc/mtab(mount table),the “mount” command uses this file to retrieve and display all the information.
/etc/mtab is a sensitive file as the kernel always track the mounted filesystem, so editing this file by hand would cause the system to lead to inconsistency and booting problem.
#cat  /proc/mounts

The above screenshot shows, the mounted filesystem details including  the mount point directory, filesystem type, flag information.
Let us see the output of /etc/mtab file
#cat  /etc/mtab

From the above output
1.The first line shows the mounted device or filesystem
2.The second line shows where the device is mounted i.e mount point directory
3.The Filesystem type i.e ext filesystem or removable media type filesystems
4.Options for mounting i.e,rw or ro
5.Dump command option,”0″ means no check
6.Fsck check order (while booting  it will check the filesystems) 0 means no check

Understanding “fstab” configuration file


This file is the most important one in Linux, the path for the fstab is located inside the /etc/fstab directory
fstab will  look for the filesystem and automatically mount them when the system boots next time.
fstab stands for filesystem table ,if you need to mount the filesystem automatically during the booting time you must put the filesystem inside /etc/fstab ,Usually after we mounting the filesystem to some mount point directory it is available for the users to access the data, now what would happen when you restart or shutdown the system?When you power on next time, All The mounted filesystems will go to unmounted state by default, Now To bring the filesystem  to active state  you have to mount one by one all the filesystems  which all are went to unmounted state,
Imagine what would happen If the disk with 100 or more mounted paritions, its really a headche for the system adminstrator to keep all these in memory, so to avoid these difficulties it’s safe to put all the important  filesystems inside the  /etc/fstab file to get automatically mount on next power on the system.
Let us have a look at the enties inside the fstab file.You can use the vi editor to edit this file if you are not comfortable with the vi editor you can use the gedit utility

Description of fstab file


1.LABEL – List the device to be mounted

2.Mount Point- Notes the directory where the filesystem will be mounted

3.Filesystem format- Describe the filesystem type i.e ext2,ext3,ext4

4.Defaults-read-only or read write, exec or noexec (exec means it allows to execute binaries,noexec means cant execute)

5.Dumpvalue- It means the data is automatically saved to disk by the dump command when you exit Linux

6.Filesystem check order(fsck check)- we use numeric values to check the filesystem 0 means ignore, nonzero means check.

#vi  /etc/fstab

the inside view of the fstab file will look like this 👇🏻

From the above 👆🏻 output some partitions and device entries have been  saved already  by the kernel, if you are adding a new filesystem or device you need to manually create a mount point  directory and insert it in to this file./dev/sda2 is not listed inside the
From the above output  /dev/sda2 is not listed inside the fstab file, so every time when you power on the system you will have to manually mount the /dev/sda2 filesystem to its mount point directory to make it visible and accessible to the users and system.
To make it mount automatically whenever you power on the system, add the /dev/sda2 details inside this fstab file.
I am going to show you how it looks before and after adding the filesystem information in to fstab file.

After login run the df -h to check the mounted filesystem details , it will not mount the /dev/sda2 filesystem automatically.

Now put the /dev/sda2 details inside the fstab file and reboot  the system, once you logged in check once again with the df -h  this time /dev/sda2 will get automatically mounted, let me show you how to add the /dev/sda2 filesystem entries inside the fstab file.

I have successfully added the /dev/sda2 filesystem entries inside the fstab file, use the tab button to go to the next field don’t use the spacebar  button to go the next filed doing so would create error and also it may  cause booting problem as  kernel will check each and every line inside the fstab  during the booting process, if no error found inside this file then after that it starts processing further program, even if you add a  dot or, any special characters (which is not required) inside the file, the system will not boot.
Now restart the system to make the changes effect,
#reboot

Now check whether /dev/sda2 is automatically mounted
#df -h

As you can see the above output  /dev/sda2  gets automatically mounted since we have mentioned the filesystem details inside the /etc/fstab file.

To display all the mount details


After mounting the partition to some mount point directory you can either use “mount command or df -h” to display all the details.
Run the mount command without any option
#mount

To mount all the filesystem mentioned in the /etc/fstab file


The filesystems mentioned in the /etc/fstab file will get automatically mounted while booting, after booting sometime  we unmount some of the filesystems listed inside /etc/fstab for some purpose.
Now if you want to mount only the filesystems that are listed inside the /etc/fstab file run the “mount”  command with the option -a, this will mount only the filesystems that are listed inside the fstab file.
#mount  -a

To unmount  only the filesystem listed in fstab


#umount -a
Note: This command is not recommeded to run in production server as it would bring the server to downstate after execution.

From the above screenshot, it has been confirmed all the filesystems mentioned inside the fstab is unmounted except “/”, coz you cannot bring the / to unmounted state, bringing the / to unmount state is like trying to  format the c:/ drive in windows OS.That is why you are getting an error message stating that “/” device is busy.

How to remount a flesystem


You can remount a mounted filesystem by using the “remount” command,usally adminstartor use this command only when they need to bring it back the filesystem status from read only to read write permission.
Let me show you one example, i have a parition /dev/sda3 and it is mounted as readonly in the mount point directory /skype, now i need to mount the filesystem  /dev/sda3  with read &write  permission.

 

Check the mounted  status of the /dev/sda3 filesystem by using the following command
#mount |grep /skype

Now remount the filesystem with read &write
#mount  -o  remount,rw   /skype

#mount   |grep   /skype

The above output shows the filesystem /dev/sda3 is now mounted with readwrite access.

How to attach the mount point to a new directory


It is possible to bind a moutpoint to another directory, doing so  the users can access the data from both the mount point directory at the same time.With mount command add – -bind option followed ny old mount point and new mpunt point.
Syntax:
#mount  -  -bind   <old mount point directory>   <new mount point directory>
#df  -h

Now bind the mount point to another directory
#mount  -  -bind   /skype   /whatsapp

Now check whether the filesystem can be access via two mount point directory
#mount  |grep  /skype

The above output shows both the mount point merged properly, Now if you do any modification in any of the mount point  it will  get update on the other mount point also.
Let me show you this with one example, Am going to create some directorys inside the /skype , after creating check with the another mount point directory(/Whatsapp) you would see the newly created directory there.
#cd  /skype
#ls
#mkdir  dir1 dir2
#ls

#cd  /whatsapp
#ls

I hope now have understood the mounting and unmounting concepts 😊😊…

##############################################################################################

 

If you found this article useful, Please Subscribe and leave your comments below, ThankYou😊😊

[rainmaker_form id=”235″]