How to Manage Partitions and File Systems in Linux: A Comprehensive Guide

by Jay

Table of Contents

Learn the essentials of managing partitions and file systems in Linux with our comprehensive guide. We cover key concepts, tools, and best practices to help you create, resize, and manage disk partitions and file systems effectively. Whether you’re a Linux beginner or an experienced sysadmin, find out how to optimize your disk space, improve system performance, and ensure data safety.

1. What is a partition in Linux?

A single hard drive can be partitioned into numerous logical drives. A contiguous group of blocks on a drive that are treated as an independent disk is known as a partition. An index that links portions of the hard drive to partitions is known as a partition table. Each operating system has specific guidelines for how it can use certain partitions while ignoring others. On the same hard drive, the various operating systems can live in harmony this way.

2. What is partitioning in Linux ?

Partitioning means dividing a single hard drive into many logical drives.

3. Why do we have multiple partitions ?

• Encapsulate our data. Since file system corruption is limited to that partition only. So we can save our data from accidents.

• We can increase disk space efficiency. Depending on our usage we can format the partition with different block sizes. So we can reduce the wastage of the disk.

• We can limit the data growth by assigning disk quotas.

4. What is the structure of the disk partition?

• The first sector of the O/S disk contains the MBR (Master Boot Record). The MBR is divided into 3 parts and its size is 512 bytes.

• The first part is IPL (Initial Program Loader) and it contains the Secondary Boot Loader. So, IPL is responsible for booting the O/S and its size is 446 bytes.

• The second part is PTI (Partition Table Information). It contains the number of partitions on the disk, the sizes of the partitions, and the type of the partitions.

5. Explain the disk partition criteria?

• Every disk can have max. 4 partitions. The 4 partitions are 3 Primary partitions and 1 Extended partition.

• The MBR and O/S will install in the Primary partition only.

• The Extended partition is a special partition and can be further divided into multiple logical partitions.

6. How to identify the disks?

In Linux, different types of disks will be identified by different naming conventions.

IDE drives will be shown as /dev/hda, /dev/hdb, /dev/hdc, …etc., and the partitions are /dev/hda1, /dev/hda2, /dev/hda3, …etc.,

iSCSI/SCSI and SATA drives will be shown as /dev/sda, /dev/sdb, /dev/sdc, …etc., and the partitions are /dev/sda1, /dev/sda2, /dev/sda3, …etc.,

Virtual drives will be shown as /dev/vda, /dev/vdb, /dev/vdc, …etc., and the partitions are /dev/vda1, /dev/vda2, /dev/vda3, …etc.,

IDE —–> Integrated Drive Electronics.
iSCSI —–> Internet Small Scale System Interface.

SCSI —–> Small Scale System Interface.

7. What is a file system?

It is a method of storing the data in an organized fashion on the disk. Every partition on the disk except MBR and An extended partition should be assigned with some file system in order to make them store the data. A file system is applied to the partition by formatting it with a particular type of file system.

8. What are the different types of file systems supported in Linux?

The Linux supported file systems are ext2, ext3, ext4, xfs, vfat, cdfs, hdfs, iso9660 …etc.

The ext2, ext3, ext4 file systems are widely used in RHEL-6 and xfs file system is introduced on RHEL-7. The vfat file system is used to maintain common storage between Linux and Windows O/S.

The cdfs file system is used to mount the CD-ROMs and the hdfs file system is used to mount DVDs.

The iso9660 file system is used to read CD/DVD.iso image format files in Linux O/S.

9. What is mounting and in how many types can we mount the partitions?

Attaching a directory to the file system in order to access the partition and its file system is known as mounting. In general, the subdirectories under /mnt directory are the mount points to mount the file systems. There are two types of mountings in Linux/Unix.

• Temporary Mounting :

In a temporary mounting first, we create a directory and mount the partition on that directory. But this type of mounting will last only till the system is up and once it is rebooted the mounting will be lost.

Example:# mount <options><device name><directory name (mount point)>

• Permanent Mounting :

In this also first we create the directory and open the /etc/fstab file and make an entry as below, <device name><mount point><file system type><mount options><take a backup or not><fsck value>

Whenever the system reboots mount the partitions according to entries in /etc/fstab file. So, these types of mountings are permanent even after the system is rebooted.

# mount -a to mount the partitions without reboot)

10. What are the differences between the ext2, ext3, ext4 and xfs file systems?

S.N. Ext2 Ext3 Ext4 Xfs
1. Stands for Second
Extended file system.
Stands for Third
Extended file system.
Stands for Fourth
Extended file system.
Stands for Extended file
system.
2. Does not having.
Journaling feature.
Supports Journaling
feature.
Supports Journaling
feature.
Supports Journaling
feature.
3. Max. file size can be.
from 16 GB to 2 TB.
Max. file size can be.
from 16 GB to 2 TB.
Max. file size can be
from 16 GB to 16 TB.
Max. file size can be
from 16 GB to 8EB.
4. Max. file system size
can be from 2 TB to 32
TB
Max. file system size
can be from 2 TB to 32
TB
Max. file system size
can be from 2 TB to 1 EB
*1EB = 1024 Peta bytes.
Max. file system size
can be from 2 TB to
16EB.
5. Cannot convert ext file
system to ext2.
We can directly convert
ext2 to ext3 file system.
We can convert all file
systems to ext4 file system.
Unmount and mount
the file system is required.

11. Which files are related to mounting in Linux?

/etc/mtab —-> is a file that stores the information of all the currently mounted file systems and this file is dynamic and keeps on changing.

/etc/fstab —-> is keeping information about the permanent mount points. If we want to make our mount point permanent then makes an entry about the mount point in this file.

/etc/fstab entries are:

1 —>Devicename

2 —>Mount point

3 —>File system type

4 —>Mount options

5 —>Take a backup should; run or not

6 —>FSCK or not

12. How to create different types of partitions?

To create different types of partition we can use fdisk command. Below is the example to create partition

# fdisk -l
# fdisk /dev/sdc
Command (m for help) : n --->  (type n for new partition)
(p - primary) or e - extended) : p --->(type p for primary partition or type e for extended partition)
First cylinder :  --->(press Enter for default first cylinder)
Last cylinder : + <size in KB/MB/GB/TB>
Command (m for help) : t --->(type t to change the partition id)
(for example 8e for Linux LVM, 82 for Linux Swap, and 83 for Linux normal partition)
Command (m for help) : w ---> (type w to save the changes into the disk)

Then update the partitioning information in partition table

# partprobe /dev/sdc1 && kpartx -a /dev/sdc1

13. How to make a file system in Linux?

# mkfs.ext2/ext3/ext4/xfs/vfat ---> <device name> ---> ( for example/dev/sdc1)

14. How to mount the file systems temporarily or permanently?

Create a directory where you want to mount the file system.

# mkdir /mnt/oracle

Use below command to mount FS temporary.

Syntex

# mount <file system> <mount point> 

Example

# mount /dev/sdc1 /mnt/oracle 

Use below command to mount File System permanently.

# vim /etc/fstab

/dev/sdc1 /mnt/oracle xfs defaults 0 0

Note: press Esc then :wq! — to Save and quit

Use Below command to mount filesystem that is added in fstab entry.

# mount -a 
OR
#mount /mnt/oracle

15. How to delete the partition?

# fdisk /dev/sdc

Command (m for help) : d ----> (type d for delete the partition)
Partition number :  --->(specify the partition number)
Command (m for help) : w--->(type w to write the changes into the disk)

Then update the partition table without restarting the system

 # partprobe /dev/sdc1 && kpartx -a /dev/sdc1 

16. The partitions are not mounting even though there are entries in /etc/fstab. How to solve this problem?

First, check if any wrong entries are there in /etc/fstab file. If all are ok then unmount all the partitions by executing the below command.

# umount -a
Then mount again mount all the partitions by executing the below command.
# mount -a

17. When trying to unmount it is not unmounting, how to troubleshoot this one?

Some times directory reflects an error while unmounting because,
(i) If you are in the same directory and trying to unmount it, check with # pwd command.

(ii) some users are present or accessing the same directory and using the contents in it, we can check this by running below command.

To check the users who are accessing that partition.

# fuser -cu <device name>

To check the files which are open in that mount point.

# lsof <device name> 

To kill that opened files.

# fuser -ck <opened file name with path>

Now we can unmount that partition using.

# umount <mount point>

18. How to see the usage information of mounted partitions?

Run below command to see device name, file system type, size, used, available size, use% and mount point

# df -hT 

19. How to see the size of the file or directory?

To see the size of the file or all the file sizes in a directory:

# du -h <filename or directory name>

To see all the file sizes located in the present working directory:

# du -h .

To see the biggest files from the current location:

# du .| sort -nr | head -n10

To see the biggest directories from a partition:

# du -s * | sort -nr | head -n10

To list the biggest files and directories (requires installation of ncdu package):

# ncdu

20. How to assign a label to the partition?

To assign a label to a partition using e2label:

# e2label <device name or partition name> <label name>

Example:

To assign “oradisk” label to /dev/sdb1 partition

# e2label /dev/sdb1 oradisk

To list all the mounted partitions along with their labels using mount:

# mount -l

21. How to mount a partition temporarily or permanently using a label?

To mount a partition using its label and mount point:

# mount LABEL=<label name> <mount point>

Example:

To mount the “oradisk” label on /mnt/oracle

# mount LABEL=oradisk /mnt/oracle

To edit the /etc/fstab file using vim:

# vim /etc/fstab

Then add the following entry:

LABEL=oradisk /mnt/oracle ext4 defaults 0 0

Press Esc, then type :+wq! to save and exit the file.

To mount all partitions listed in /etc/fstab:

# mount -a

To verify whether partitions are mounted or not:

# mount

22. How mount the partition permanently using block id (UUID)?

To see the UUID or block ID of a partition using blkid:

# blkid <partition name or disk name>

Example:

To see the UUID or block ID of the /dev/sdb2 partition

# blkid /dev/sdb2

To copy the UUID from the output and make an entry in /etc/fstab using vim:

# vim /etc/fstab

Then add the following entry:

UUID="{UUID}" /mnt/oracle ext4 defaults 0 0

Replace “{UUID}” with the actual UUID value copied from blkid output.

Press Esc, then type :+wq! to save and exit the file.

23. What is the basic rule for swap size?

(i) If the size of the RAM is less than or equal to 2GB, then the size of the swap = 2 X RAM size.

(ii) If the size of the RAM is more than 2GB, then the size of the swap = 2GB + RAM size.

24. How to create a swap partition and mount it permanently?

To see the present swap size:

# free -m

To see the swap usage:

# swapon -s

To create a new partition using fdisk:

# fdisk <disk name>

Example:

# fdisk /dev/sdb
  Command (m for help): n (to create a new partition)
  First cylinder: (press Enter to take as default value)
  Last cylinder: +2048M (to create a 2GB partition)
  Command (m for help): t (to change the partition ID)
  Enter the partition number: 2 (to change the /dev/sdb2 partition ID)
  Enter the ID: 82 (to change the partition ID to Linux Swap)
  Command (m for help): w (to save the changes to the disk)

To update the partition table information:

# partprobe /dev/sdb

To format the partition with swap file system using mkswap:

# mkswap <device or partition name>

Example:

To format the /dev/sdb2 partition with swap file system

# mkswap /dev/sdb2

To activate the swap space using swapon:

# swapon <device or partition name>

Example:

To activate the /dev/sdb2 swap space

# swapon /dev/sdb2

To see the updated swap size:

# free -m

To make an entry in /etc/fstab to permanently mount the swap partition using vim:

# vim /etc/fstab

Then add the following entry:

/dev/sdb2 swap swap defaults 0 0

Press Esc, then type :+wq! to save and exit the file.

25. What are the attributes of the file system?

(i) Inode number
(ii) File name

(iii) Data block

26. What is an inode number and what is its use of it?

Inode numbers are the objects the Linux O/S uses to record the information about the file.
Generally, an inode number contains two parts.

(a) Inode first part contains information about the file, its owner, its size, and its permissions.
(b) Inode second part contains a pointer to data blocks associated with the file content.

That’s why using the inode number we can get the file information quickly.

27. How to check the integrity of a file system or the consistency of the file system?

By running the # fsck <device or partition name>command we can check the integrity of the file system.
But before running the fsck command first unmount that partition and then run the fsck command.

28. What is fsck check or what are the phases of the fsck?

(a) First it checks the blocks and sizes of the file system
(b) Second it checks file system path names
(c) Third it checks file system connectivity
(d) Fourth it checks file system reference counts (nothing but inode numbers)
(e) Finally it checks file system occupied cylindrical groups

29. Why the file system should be unmounted before running the fsck command?

If we run fsck on mounted file systems, it leaves the file systems in an unusable state and also deletes the data. So, before running the fsck command the file system should be unmounted.

30. Which type of file system problems do you face?

(i) File system full

(ii) File system corrupted

31. How to extend the root file system which is not on LVM?

By using the # gparted command we can extend the root partition, otherwise, we cannot extend the file systems that are not on LVM.

32. How to unmount a file system forcefully?

# umount -f <mount point>
# fuser -ck <mount point>

33. How to know the file system type?

Bellow command gives the file system type information

# df -hT 

34. How to know which file system occupies more space and top 10 file systems?

# df -h <device or partition name> | sort -r | head -10

35. What is the command to know the mounted file systems?

# mount or # cat /etc/mtab

36. How to know whether the file system is corrupted or not?

First, unmount the file systems and then run # fsck command on that file system.

37. How to recover if a file system is corrupted or crashed?

If the normal or not related O/S file system is corrupted first unmount that file system and run fsck command on that file system and if the O/S related file system is corrupted then boot the system with CDROM in single-user mode and run the fsck command. If the normal or not related to the O/S file system crashed then restore it from the recent backup and if the O/S related file system is crashed then boot the system with CDROM in single-user mode and restore it from the recent backup.

38. How to create a file with a particular size?

To create 500MB size /orafile with 4KB blocksize

# dd if=/dev/zero of=/orafile bs=1MB count=500 

39. How to find how many disks are attached to the system?

To see how many disks are attached to the system

# fdisk -l 

40. What is journaling?

It is a dedicated area in the file system where all the changes are tracked when the system crashed. So the possibility of file system corruption or crashes is less because of this journaling feature.

41. How to repair the Superblock of the file system?

Whenever we want to store the data on the hard disk, if the input/output error occurs then the Superblock of the file system may be erased or corrupted. So, we have to restore or repair that Superblock.

Run below command to unmount a file system:

# umount <file system mount point>

Now list the superblocks of a file system using dumpe2fs and grep:

# dumpe2fs </dev/vgname/lvname> | grep superblock

Note: This will list the superblocks, including the primary superblock and any secondary superblocks.

Now restore a damaged superblock:

# e2fsck -b <copy and paste the secondary superblock from the above list> </dev/vgname/lvname>

Note: Replace <copy and paste the secondary superblock from the above list> with the actual secondary superblock value obtained from the previous step, and </dev/vgname/lvname> with the actual file system device or logical volume name.

Now try to mount all file systems defined in /etc/fstab using mount:

# mount -a

42. How to create the file systems with the user-specified superblock reserve space?

Below command use to format the partition with <no.>% of reserve space to the superblock

# mkfs.ext4 -m <no.><partition name> 

Note: Whenever we format the file system, by default it reserves the 5% partition space for Superblock.

43. How to modify the superblock reserve space?

Below command use to modify the superblock reserve space to <no.>%

# tune2fs -m <no.><partition name> 

Important Commands :

To check the consistency of the file system:

# fsck <partition name>

To check the consistency of the file system in interactive mode:

# e2fsck <partition name>

To check the consistency of the file system without interactive mode:

# e2fsck -p <partition name>

To see the superblock information:

# mke2fs -n <partition name>

To format the partition in the specified file system type:

# mke2fs -t <file system type><partition name>

To format the partition in default ext2 file system type:

# mke2fs <partition name>

To check the block size of the /dev/sdb1 file system:

# blockdev --getbs /dev/sdb1

To check and repair the file system:

# fsck <device or partition name>

Note: Before running any fsck command, make sure to unmount the partition.

To check the consistency of the file system:

# fsck <partition name>

To unmount all file systems except the root file system:

# umount -a

To mount all file systems listed in the /etc/fstab file:

# mount -a

To run fsck on all file systems:

# fsck -A

To run fsck on all file systems without asking any questions:

# fsck -AR -y

To run fsck on all ext3 file systems:

# fsck -AR -t ext3 -y

To run fsck on all file systems except ext3 file systems:

# fsck -AR -t noext3 -y

To see the details of the /dev/sdb1 partition without actually running fsck:

# fsck -n /dev/sdb1

To check whether the journaling is there or not:

# tune2fs -l /dev/sdb1

To convert ext2 file system to ext3 file system:

# tune2fs -j /dev/sdb1

To check whether the journaling is added or not:

# tune2fs -l /dev/sdb1

To convert ext3 file system to ext2 file system:

# tune2fs -O has_journal /dev/sdb1

To convert ext2 file system to ext4 file system:

# tune2fs -O dir_index, has_journal, unit_bg /dev/sdb1

To convert ext3 file system to ext4 file system:

# tune2fs -O extents, dir_index, unit_bg /dev/sdb1

To mount the partition with read and write permissions:

# mount -o remount, rw /dev/sdb1

To mount the partition with read-only permissions:

# mount -o remount, ro /dev/sdb1

To check whether this directory is a mount point or a normal directory:

# mount <mount point or directory name>

To check the metadata of the partition and repair the metadata:

# dump2fs <device or partition name>

To list total hard disks attached to the system and their partitions:

# fdisk -l

To see the users who are accessing that file system:

# fuser -cu <device or partition name>

To kill the user’s processes who are accessing the file system:

# fuser -cK <device or partition name>

Note: Even though we kill those user’s processes sometimes we cannot unmount those partitions, so if this situation arises then first see the process id’s of the user-opened files by # lsof <mount point> and kill those processes forcefully by # kill -9 <process id>

To watch the system activity report using sar utility:

# sar

To check the health of the specified hard disk:

# smartctl -H <hard disk name>

To see the information of the specified hard disk:

# smartctl -i <hard disk name>

To get more information about the specified hard disk:

# smartctl -a <hard disk name>

To track all the log files between two different timings and save them in /run/log location:

# journalctl

* /run/log is mounted on tmpfs file system. ie., if the system is rebooted, the whole information in
that location will be deleted or erased.
* We can change the location of the /run/log to another like /var/log/journal by using below commands.

To make a directory in /var/log location:

# mkdir -p /var/log/journal

To change the group ownership of /var/log/journal:

# chown root:systemd-journal /var/log/journal

To set the sgid on /var/log/journal:

# chmod g+s /var/log/journal

To kill old /run/log process and change the location of journal messages to /var/log/journal:

# killall -USR1 systemd-journald

To display last five lines of all the log files:

# journalctl -n 5

To display all the error messages:

# journalctl -p err

To watch journalctl messages continuously:

# journalctl -f

To see all the journalctl messages since today or yesterday:

# journalctl --since today

# journalctl --since yesterday

To see the journal messages between the specified two dates:

# journalctl --since "date1" --until "date2"

To see the process name of PID 1:

# journalctl -pid=1

To see the audit report:

# auditctl

Note: In the above command results go to the Value and Tresh fields. If Tresh is more than the Value then an immediately hard disk change is required. If Tresh is lower than the Value then the hard disk is ok. So, the Tresh is always lower than the Value, otherwise, hard disk change is required immediately and reported to this manager.

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.