Disk Partitioning in Linux Using parted Command

The parted command is a flexible disk partitioning tool used to create, modify, and manage disk partitions. Unlike fdisk, which is limited to MBR partition tables, parted supports both MBR (Master Boot Record) and GPT (GUID partition Table) - making it ideal for managing large disk (>2TB) and advanced storage configurations.

Why use parted Instead of fdisk

  • GPT Support: Works with both MBR and GPT partition tables.

  • Large Disk Support: Handles disks larger than 2TB.

  • Scripting-Friendly: Ideal for automation and scripting.

  • Dynamic Resizing: Resize partition without unmounting

Checking Disk and partition Tables

  • List All Disks:

$ sudo parted -l

This shows all disks, partitions and partition tables (MBR/GPT).

Partitioning a Disk with parted

Step1:

Start a parted interactive mode

$ sudo parted /dev/sdb # replace /dev/sdb/ with your target disk


You'll enter the parted interactive shell:

GNU Parted 3.2

Using /dev/sdb

(parted)


Step2: 

Set the Partition Table Type

For a new disk without a partition table:

(parted) mklabel gpt


  • Use gpt for large disk (>2TB)

  • Use msdos MBR (Older systems)

Check the current partition table:


(parted) print


Step3:

 Create a New Partition

(parted) mkpart primary ext4 1MB 2GB


  • primary → Partition type

  • ext4 → Intended file system type (optional here)

  • 1MB → Starting Point (Aligns to MB boundary for performance)

  • 20GB → Ending Size


Note: Always start at 1MB for Alignment, especially with SSDs.


Step4:

View Partitions

(parted) print


Step5:

Write changes and exit

(parted) quit


Changes are automatically saved in parted (no need to write like in fdisk)


Note:

If you want to delete a partition use

(parted) rm 1 # 1 is partition number

Formatting the new Partition

After creating partitions, format them with the desired file system


  • ext4 

$ sudo mkfs.ext4 /dev/sdb1


  • xfs

$ sudo mkfs.xfs /dev/sdb1

Mounting the Partition


  • Create a mount point directory:

$ sudo mkdir /mnt/directory1


  • Mount the partition:

$ sudo mount /dev/sdb1 /mnt/directory1


  • Verify:

$ lsblk

$ df -Th

Make Mount persistent after reboot

  • Edit /etc/fstab file

$ sudo vim /etc/fstab

  • Add following content and save the file


/dev/sdb1    /mnt/directory1    ext4    defaults    0    0


  • Apply the changes without rebooting

$ sudo mount -a

Resizing Partitions with parted

  • Resize the Partition

$ sudo parted /dev/sdb

(parted) resizepart 1 20GB # resizes partition 1 to 20 GB


  • Resize the File System (ext4)

$ sudo resize2fs /dev/sdb1


Resize the File System (xfs)

$ sudo xfs_growfs /dev/sdb1

Scripting with parted (non-interactive mode)

$ sudo parted -s /dev/sdb mklabel gpt mkpart primary ext4 1MiB 20GiB


  • -s : Script mode (no prompts)

  • Perfect for automation tools like Ansible or bash scripts

Common Errors and troubleshooting

  • Device is busy

Unmount the mount point first

$ sudo umount /dev/sdb1


  • Check for the open files

$ sudo lsof /dev/sdb1


  • Partition Not Recognized after changes

Re-read partition table:

$ sudo partprobe


Important parted Command References

Command

Description

mklabel gpt

Create a GPT partition table

mkpart primary ext4 1MiB 2GiB

Create a new partition

print 

Show current partitions

rm 1

Delete partition number 1

resizepart 1 5GiB

Resize the partition 1 to 5GiB

set 1 boot on

Set the boot flag on partition 1

quit

Exited parted interactive mod

align-check optimal 1

Check alignment of the partition 1


MBR vs GPT : Key Differences


Feature

MBR (msdos)

GPT

Max Partition Size

2TB

9.4ZB (Zettabytes)

Partitions Limit

4 primary or 

3 primary and 1 extended

128+

Boot Compatibility

Legacy BIOS

UEFI (Modern Systems)

Redundancy

NO

Yes (backup table)

Conclusion

The parted command is a powerful and versatile tool for managing disk partitions, especially on large drives or GPT-based systems. It offers advanced features like dynamic resizing, alignment checks, and scripting capabilities that make it suitable for both manual and automated environments.

No comments:

Post a Comment