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:
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
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
MBR vs GPT : Key Differences
No comments:
Post a Comment