Disk Partitioning in Linux using fdisk Command

The fdisk command is a powerful tool for managing disk partitions in Linux. It allows you to create, delete, modify and view partitions on hard drives. It works with MBR (Master Boot Record) partition tables, and for GPT (GUID Partition Table) disks, you'd typically use gdisk or parted commands.

Understanding Disk Partitioning


Partition: A logical division of a storage device. Each Partition can hold a file system and data.

MBR vs GPT:

  • MBR: Supports up to 4 primary partitions (3 primary + 1 Extended)

  • GPT: Supports unlimited partitions (Usually up to 128 in Linux)  

Checking Available Disks:

Before starting, identify the disk attached to your system:


$ lsblk # lists all block devices

$ fdisk -l # lists disk partitions and details

Important fdisk commands summary


Command

Description

> m

Show help menu

> p

Display/Print partition table

> n

Create a New Partition

> d

Delete a Partition

> t

Change Partition type

> w

Write changes to disk

> q

Quit without saving changes

> L

List partition type codes


Partitioning a Disk with fdisk 

Step1:

Select the Disk to be partitioned and enter to the fdisk interactive mode.

$ fdisk /dev/sdb

# Replace /dev/sdb with your disk.


Step2:

View Existing Partitions

> p 

# This displays the current partition table.


Step3:

Create a New Partitions

  1. Press n to create a new partition.

  2. Choose p for primary partition or e for extended

  3. Enter the partition number (1-4 for MBR)

  4. Specify the starting sector (Press enter to accept default)

  5. Specify the ending sector or size (e.g., +20G for 20GB)

Step4: 

Change Partition Type (Optional)

For Example, to set it as a Linux LVM partition:

> L # To List all available partition types

> t

<Partition Number> # Choose partition 

> 8e # Code for Linux LVM


Step5:

Write Changes to Disk

After creating/modifying partitions:

> w # Writes changes to disk and exits fdisk

> q # To Discard the changes and quit from fdisk interactive mode.

Formatting the New partition

After creating the partition, format it with a file system:

  • EXT4 (Common Linux File System)

$ sudo mkfs.ext4 /dev/sdb1


  • XFS (default in RHEL/CentOS7+)

$ sudo mkfs.xfs /dev/sdb1

Mounting The Partition

Step1:

Create a mount point directory

$ sudo mkdir /mnt/mydata

Step2:

Mount the Partition

$ sudo mount /dev/sdb1 /mnt/mydata

Step3:

Verify the Partition directory

$ df -Th

Make the Mount Persistent (After Reboot)

  • Edit the /etc/fstab file

$ sudo vim /etc/fstab

  • Add content to the file as follows and save it.

/dev/sdb1    /mnt/mydata    ext4    defaults    0    0

Deleting a Partition

  • Open a fdisk interactive mode

$ sudo fdisk /dev/sdb

  • List the partition and choose a partition to delete

> p

  • Delete the partition:

> d 

<Enter the partition number to delete>

  • Write Changes and quit 

> w

Important Considerations

  • Backup Data: Partitioning can lead to data loss if not done carefully.

  • Unmount Partitions: Always unmount partitions before modifying them.

  • Partition Alignment: For SSDs, Proper alignment improves performance.

  • GPT Disks: Use parted or gdisk for GPT-Based disk.

Troubleshooting Common Errors

  • Device is busy

Ensure the partition is unmounted before making changes

$ sudo umount /dev/sdb1

  • Use lsof to check if processes are using it.

$ sudo lsof /dev/sdb1


  • Partition not recognized after creating

    • Rescan the disk

$ sudo partprobe

Conclusion

The fdisk tool is essential for managing disk partitions in Linux, especially for MBR-Based disks. While powerful, it requires caution to avoid data loss. For more complex setups (like GPT, large disks, or advanced configurations), consider using tolls like parted, lsblk, gparted (GUI-based).


No comments:

Post a Comment