Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Format a Hard Disk on Linux OS
In this article, we will learn how to format and configure a hard disk on Linux OS. When a new drive is installed and visible to the BIOS, it is automatically detected by the operating system. Disk drives are assigned device names beginning with hd or sd followed by a letter indicating the device order (e.g., /dev/sda, /dev/sdb).
Detecting the New Hard Drive
First, verify that the new drive is detected by listing all storage devices:
# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sdb
This output shows that /dev/sda is divided into two partitions (/dev/sda1 and /dev/sda2), while the new drive /dev/sdb has no partitions yet. Now we can proceed to create partitions and format the drive.
Creating Linux Partitions
Use the fdisk utility to create partitions on the new disk drive:
# fdisk /dev/sdb
The fdisk utility will display warnings about the new disk and enter interactive mode. Switch off DOS compatibility mode and change units to sectors for better precision:
Command (m for help): c DOS Compatibility flag is not set Command (m for help): u Changing display/entry units to sectors
View the current partition table with the p command:
Command (m for help): p Disk /dev/sdb: 34.4 GB, 34359738368 bytes 255 heads, 63 sectors/track, 4177 cylinders Units = sectors of 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes
Creating a New Partition
Create a new primary partition using the n command:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-67108863, default 2048): [Press Enter]
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-67108863, default 67108863): [Press Enter]
Using default value 67108863
Write the partition table to disk using the w command:
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Verify the new partition is created:
# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1
Creating a File System
Format the new partition with the ext4 file system using mkfs.ext4:
# mkfs.ext4 -L backup /dev/sdb1
mke2fs 1.41.12 (17-May-2010) Filesystem label=backup OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 2097152 inodes, 8388352 blocks 419417 blocks (5.00%) reserved for the super user Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
Mounting the File System
Create a mount point directory and mount the new partition:
# mkdir /data # mount /dev/sdb1 /data
Verify the filesystem is mounted:
# mount | grep sdb1 /dev/sdb1 on /data type ext4 (rw)
Configuring Automatic Mount at Boot
To automatically mount the filesystem at boot time, add an entry to /etc/fstab:
# echo "LABEL=backup /data ext4 defaults 1 2" >> /etc/fstab
Sample /etc/fstab configuration showing the new entry:
/dev/mapper/vg_rhel6-lv_root / ext4 defaults 1 1 UUID=4a9886f5-9545-406a-a694-04a60b24df84 /boot ext4 defaults 1 2 /dev/mapper/vg_rhel6-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 LABEL=backup /data ext4 defaults 1 2
Test the fstab configuration by unmounting and remounting:
# umount /data # mount -a
Key Points
fdisk is used for partitioning disks
mkfs.ext4 creates ext4 filesystems
/etc/fstab configures automatic mounting at boot
Always test mount configurations before rebooting
Conclusion
Formatting a hard disk on Linux involves partitioning with fdisk, creating a filesystem with mkfs, mounting the partition, and configuring automatic mounting in /etc/fstab. This process provides additional storage space that persists across system reboots.
