
Table of Contents
- Linux disk management in Linux
- 1. What is a partition in Linux?
- 2. What is partitioning in Linux ?
- 3. Why do we have multiple partitions?
- 4. What is the structure of the disk partition?
- 5. Explain the disk partition criteria?
- 6. How to Identify the Disks?
- 7. What is a file system?
- 8. What are the different types of file systems supported in Linux?
- 9. What is mounting and in how many types can we mount the partitions?
- 10. What are the differences between the ext2, ext3, ext4 and xfs file systems?
- 11. Which files are related to mounting in Linux?
- 12. How to create different types of partitions?
- 13. How to make a file system in Linux?
- 14. How to mount the file systems temporarily or permanently?
- 15. How to delete the partition?
- 16. The partitions are not mounting even though there are entries in /etc/fstab. How to solve this problem?
- 17. When trying to unmount it is not unmounting, how to troubleshoot this one?
- 18. How to see the usage information of mounted partitions?
- 19. How to see the size of the file or directory?
- 20. How to assign a label to the partition?
- 21. How to mount a partition temporarily or permanently using a label?
- 22. How mount the partition permanently using block id (UUID)?
- 23. What is the basic rule for swap size?
- 24. How to create a swap partition and mount it permanently?
- 25. What are the attributes of the file system?
- 26. What is an inode number, and what is it used for?
- 27. How to check the integrity of a file system or the consistency of the file system?
- 28. What is fsck check or what are the phases of the fsck?
- 29. Why the file system should be unmounted before running the fsck command?
- 30. Which type of file system problems do you face?
- 31. How to extend the root file system which is not on LVM?
- 32. How to unmount a file system forcefully?
- 33. How to know the file system type?
- 34. How to know which file system occupies more space and top 10 file systems?
- 35. What is the command to know the mounted file systems?
- 36. How to know whether the file system is corrupted or not?
- 37. How to recover if a file system is corrupted or crashed?
- 38. How to create a file with a particular size?
- 39. How to find how many disks are attached to the system?
- 40. What is journaling?
- 41. How to repair the Superblock of the file system?
- 42. How to create the file systems with the user-specified superblock reserve space?
- 43. How to modify the superblock reserve space?
Linux disk management in Linux
Linux disk management is an essential skill for system administrators and users who need to optimize their systems or troubleshoot storage-related issues. This Q&A guide covers the fundamental concepts of managing disk partitions, filesystems, and storage devices in Linux. Whether you’re working with file integrity, resizing partitions, or mounting disks, this guide provides clear, actionable answers to common questions and practical tips to help you manage your Linux system’s storage efficiently. From basic disk operations to advanced configuration techniques, you’ll gain insights into maintaining a smooth and reliable disk setup.
1. What is a partition in Linux?
A single hard drive can be partitioned into multiple logical drives. A partition is a contiguous group of blocks on a drive that are treated as an independent disk. The partition table is an index that links portions of the hard drive to partitions. Each operating system has specific guidelines for how it can use certain partitions while ignoring others. In this way, multiple operating systems can coexist on the same hard drive.
2. What is partitioning in Linux ?
Partitioning is the process of dividing a single physical hard drive into multiple logical segments or drives.
3. Why do we have multiple partitions?
- Data Encapsulation: File system corruption affects only one partition.
- Disk Efficiency: Format partitions with different block sizes depending on usage.
- Data Growth Management: Apply disk quotas to limit usage.
4. What is the structure of the disk partition?
- Master Boot Record (MBR):
- IPL (Initial Program Loader): 446 bytes, responsible for booting the OS.
- Partition Table Information (PTI): 64 bytes, contains details about up to 4 partitions (size, type, location).
- Boot Signature: Last 2 bytes (
0x55AA
), indicates a valid MBR.
- GUID Partition Table (GPT) (for newer systems):
- Protective MBR: Ensures compatibility with older systems.
- GPT Header: Contains metadata (signature, version, etc.).
- Partition Entry Array: Stores information about up to 128 partitions.
- CRC32 Checksums: Ensures data integrity of the GPT header and partition entries.
- Partitions: Logical sections that contain data, each with a start/end sector and file system type (e.g., NTFS, EXT4).
- Boot Sector (for bootable partitions): Contains bootloader code necessary to start the OS.
- Swap Partition (Optional): Used for virtual memory, typically in Linux.
5. Explain the disk partition criteria?
- Maximum of 4 partitions (3 Primary + 1 Extended).
- OS and MBR install in Primary partition.
- Extended partitions can host multiple logical partitions.
6. How to Identify the Disks?
In Linux, different types of disks are identified using distinct naming conventions:
- IDE Drives: These are identified as
/dev/hda
,/dev/hdb
,/dev/hdc
, and so on. The partitions are named/dev/hda1
,/dev/hda2
,/dev/hda3
, etc. - iSCSI/SCSI and SATA Drives: These are identified as
/dev/sda
,/dev/sdb
,/dev/sdc
, and so on. The partitions are named/dev/sda1
,/dev/sda2
,/dev/sda3
, etc. - Virtual Drives: These are identified as
/dev/vda
,/dev/vdb
,/dev/vdc
, and so on. The partitions are named/dev/vda1
,/dev/vda2
,/dev/vda3
, etc.
Acronyms:
- IDE: Integrated Drive Electronics
- iSCSI: Internet Small Computer System Interface
- SCSI: Small Computer System Interface
7. What is a file system?
It is a method of storing data in an organized fashion on the disk. Every partition on the disk, except the MBR and extended partition, should be assigned a file system in order to store data. A file system is applied to the partition by formatting it with a specific type of file system.
8. What are the different types of file systems supported in Linux?
Linux supports various file systems, including ext2, ext3, ext4, xfs, vfat, cdfs, hdfs, iso9660, and more.
- ext2, ext3, ext4: These file systems are commonly used in RHEL 6 and earlier versions, with ext4 being the most widely used.
- xfs: Introduced in RHEL 7, XFS is a high-performance file system, particularly suited for large file systems.
- vfat: This file system is used for shared storage between Linux and Windows operating systems.
- cdfs: Used to mount CD-ROMs.
- hdfs: Used for mounting DVDs.
- iso9660: This file system is used for reading CD/DVD .iso image files in Linux.
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.
- 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.
Example for temporary mounting:
mount /dev/sdc1 /mnt/mydir
Permanent mounting:
Edit /etc/fstab:
/dev/sdc1 /mnt/mydir xfs defaults 0 0
Mount using:
mount -a
10. What are the differences between the ext2, ext3, ext4 and xfs file systems?
Feature | Ext2 | Ext3 | Ext4 | XFS |
---|---|---|---|---|
Stands for | Second Extended File System | Third Extended File System | Fourth Extended File System | Extended File System |
Journaling Feature | Does not have journaling feature | Supports journaling feature | Supports journaling feature | Supports journaling feature |
Max File Size | From 16 GB to 2 TB | From 16 GB to 2 TB | From 16 GB to 16 TB | From 16 GB to 8 EB |
Max File System Size | From 2 TB to 32 TB | From 2 TB to 32 TB | From 2 TB to 1 EB | From 2 TB to 16 EB |
File System Conversion | Cannot convert to Ext3 or Ext4 | Can convert to Ext4 | Can convert to Ext4 | Unmount and mount required |
- /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?
- Create or identify a partition or disk
You can use tools likefdisk
,parted
, orlsblk
to view or create partitions. - Use the
mkfs
command
Themkfs
(make file system) command is used to format a partition with a specific file system type.
🔧 Examples of Creating File Systems:
- ext4 File System:
mkfs.ext4 /dev/sdX1
- xfs File System:
mkfs.xfs /dev/sdX1
- vfat File System (for USB drives, etc.):
mkfs.vfat /dev/sdX1
Replace /dev/sdX1
with your actual partition name (e.g., /dev/sdb1
).
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 -aThen 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,
- If you are in the same directory and trying to unmount it, check with # pwd command.
- 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?
- If the size of the RAM is less than or equal to 2GB, then the size of the swap = 2 X RAM size.
- 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?
- Inode number
- File name
- Data block
26. What is an inode number, and what is it used for?
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?
- First it checks the blocks and sizes of the file system
- Second it checks file system path names
- Third it checks file system connectivity
- Fourth it checks file system reference counts (nothing but inode numbers)
- 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?
- File system full
- 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>
Some 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.
Read Also | 7 Best CentOS Alternatives: Find the Right Linux Distro for You