Comprehensive Guide to Disk Partitioning with Parted
Table of Contents
1. [Introduction to Parted](#introduction-to-parted) 2. [Installation and Prerequisites](#installation-and-prerequisites) 3. [Understanding Disk Partitioning Concepts](#understanding-disk-partitioning-concepts) 4. [Parted Command Syntax and Options](#parted-command-syntax-and-options) 5. [Interactive vs Non-Interactive Mode](#interactive-vs-non-interactive-mode) 6. [Partition Table Types](#partition-table-types) 7. [Basic Parted Operations](#basic-parted-operations) 8. [Advanced Partitioning Tasks](#advanced-partitioning-tasks) 9. [Practical Examples](#practical-examples) 10. [Best Practices and Safety Considerations](#best-practices-and-safety-considerations) 11. [Troubleshooting Common Issues](#troubleshooting-common-issues) 12. [Comparison with Other Tools](#comparison-with-other-tools)Introduction to Parted
Parted (GNU Parted) is a powerful command-line disk partitioning utility for Linux and Unix-like systems. It provides comprehensive functionality for creating, resizing, moving, and managing disk partitions on various storage devices. Unlike traditional partitioning tools like fdisk, parted supports both MBR (Master Boot Record) and GPT (GUID Partition Table) partition schemes, making it suitable for modern systems with large drives.
Key Features of Parted
| Feature | Description | |---------|-------------| | Multi-format Support | Works with MBR, GPT, and other partition table formats | | Large Disk Support | Handles disks larger than 2TB efficiently | | Resize Capabilities | Can resize partitions without data loss (with limitations) | | Scriptable | Supports both interactive and batch modes | | Cross-platform | Available on multiple Unix-like operating systems | | Real-time Operations | Changes are applied immediately to the disk |
When to Use Parted
Parted is ideal for: - Setting up new storage devices - Modifying existing partition layouts - Converting between partition table types - Automating partition management in scripts - Working with large disks (>2TB) - Managing complex partition schemes
Installation and Prerequisites
Installing Parted
Parted comes pre-installed on most Linux distributions. If not available, install it using your distribution's package manager:
#### Ubuntu/Debian
`bash
sudo apt update
sudo apt install parted
`
#### Red Hat/CentOS/Fedora
`bash
For RHEL/CentOS
sudo yum install partedFor Fedora
sudo dnf install parted`#### Arch Linux
`bash
sudo pacman -S parted
`
Prerequisites and Requirements
Before using parted, ensure you have:
| Requirement | Description | |-------------|-------------| | Root Privileges | Most partitioning operations require superuser access | | Backup Strategy | Always backup important data before partitioning | | Unmounted Partitions | Target partitions should be unmounted before modification | | System Knowledge | Understanding of partition types and file systems | | Recovery Plan | Know how to recover from potential mistakes |
Verifying Installation
Check if parted is installed and view version information:
`bash
parted --version
`
Expected output:
`
parted (GNU parted) 3.4
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later `
Understanding Disk Partitioning Concepts
Partition Tables
A partition table is a data structure that defines how the disk space is divided into partitions. Different partition table formats have varying capabilities and limitations.
#### MBR (Master Boot Record)
| Characteristic | Details | |----------------|---------| | Maximum Disk Size | 2TB | | Maximum Partitions | 4 primary partitions | | Extended Partitions | Support for logical partitions within extended partition | | Boot Support | Traditional BIOS boot | | Compatibility | Excellent with older systems |
#### GPT (GUID Partition Table)
| Characteristic | Details | |----------------|---------| | Maximum Disk Size | 9.4 ZB (practically unlimited) | | Maximum Partitions | 128 partitions by default | | Redundancy | Backup partition table at end of disk | | Boot Support | UEFI boot support | | Modern Features | Partition names, unique GUIDs |
Partition Types
#### Primary Partitions - Can contain an operating system - Limited to 4 on MBR disks - Can be made bootable
#### Extended Partitions - Container for logical partitions - Only available on MBR - Cannot contain data directly
#### Logical Partitions - Created within extended partitions - Allow more than 4 partitions on MBR - Cannot be used for booting (traditionally)
Parted Command Syntax and Options
Basic Syntax
`bash
parted [options] [device [command [parameters]...]]
`
Command-Line Options
| Option | Long Form | Description | |--------|-----------|-------------| | -h | --help | Display help information | | -l | --list | List partition layout on all block devices | | -m | --machine | Display machine parseable output | | -s | --script | Never prompt for user intervention | | -v | --version | Display version information | | -a | --align | Set alignment for newly created partitions |
Alignment Options
| Alignment Type | Description | Use Case | |----------------|-------------|----------| | none | No alignment | Legacy systems, maximum space usage | | cylinder | Align to cylinder boundaries | Old hardware compatibility | | minimal | Minimal alignment for performance | General use | | optimal | Optimal alignment for performance | Recommended for SSDs and modern drives |
Interactive vs Non-Interactive Mode
Interactive Mode
Interactive mode provides a command prompt where you can execute multiple commands on a device:
`bash
sudo parted /dev/sdb
`
This opens the parted prompt:
`
GNU Parted 3.4
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
`
Non-Interactive Mode
Non-interactive mode executes a single command and exits:
`bash
sudo parted /dev/sdb print
`
Script Mode
Script mode suppresses prompts and is ideal for automation:
`bash
sudo parted -s /dev/sdb mklabel gpt
sudo parted -s /dev/sdb mkpart primary ext4 1MiB 100%
`
Partition Table Types
Creating Partition Tables
#### GPT Partition Table
`bash
sudo parted /dev/sdb mklabel gpt
`
#### MBR Partition Table
`bash
sudo parted /dev/sdb mklabel msdos
`
Supported Partition Table Types
| Type | Description | Common Use | |------|-------------|------------| | gpt | GUID Partition Table | Modern systems, large disks | | msdos | MS-DOS partition table (MBR) | Legacy compatibility | | dvh | SGI disk volume header | SGI systems | | mac | Apple partition map | Mac systems | | pc98 | NEC PC-98 partition table | Japanese PC-98 systems | | sun | Sun disk label | Sun/SPARC systems | | loop | Raw disk access | Special purposes |
Basic Parted Operations
Viewing Disk Information
#### List All Devices
`bash
sudo parted -l
`
#### View Specific Device
`bash
sudo parted /dev/sdb print
`
Sample output:
`
Model: ATA SAMSUNG SSD (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 538MB 537MB fat32 EFI boot, esp
2 538MB 500GB 500GB ext4 root
`
#### Machine-Readable Output
`bash
sudo parted -m /dev/sdb print
`
Output format:
`
/dev/sdb:500GB:scsi:512:512:gpt:ATA SAMSUNG SSD:;
1:1049kB:538MB:537MB:fat32:EFI:boot, esp;
2:538MB:500GB:500GB:ext4:root:;
`
Creating Partitions
#### Basic Partition Creation
`bash
sudo parted /dev/sdb mkpart primary ext4 1MiB 10GiB
`
#### Named Partition (GPT)
`bash
sudo parted /dev/sdb mkpart "System Root" ext4 1GiB 50GiB
`
#### Partition with Specific File System
`bash
sudo parted /dev/sdb mkpart logical fat32 50GiB 60GiB
`
Partition Creation Parameters
| Parameter | Description | Example | |-----------|-------------|---------| | Partition Type | primary, logical, extended | primary | | File System | File system type | ext4, fat32, ntfs | | Start | Starting position | 1MiB, 100MB, 1GiB | | End | Ending position | 10GiB, 50%, 100% |
Deleting Partitions
`bash
sudo parted /dev/sdb rm 2
`
This command removes partition number 2 from the device.
Resizing Partitions
#### Growing a Partition
`bash
sudo parted /dev/sdb resizepart 2 100%
`
#### Shrinking a Partition
`bash
sudo parted /dev/sdb resizepart 2 50GiB
`
Important Note: Parted can resize the partition table entry, but it cannot resize the file system. You must use appropriate file system tools (resize2fs, xfs_growfs, etc.) separately.
Advanced Partitioning Tasks
Setting Partition Flags
#### Boot Flag
`bash
sudo parted /dev/sdb set 1 boot on
`
#### ESP (EFI System Partition) Flag
`bash
sudo parted /dev/sdb set 1 esp on
`
#### LVM Flag
`bash
sudo parted /dev/sdb set 2 lvm on
`
Common Partition Flags
| Flag | Purpose | Typical Use | |------|---------|-------------| | boot | Bootable partition | System boot partition | | esp | EFI System Partition | UEFI boot partition | | lvm | LVM partition | Logical Volume Management | | raid | RAID partition | Software RAID arrays | | swap | Swap partition | Virtual memory | | hidden | Hidden partition | OEM recovery partitions |
Moving Partitions
`bash
sudo parted /dev/sdb move 2 10GiB
`
Warning: Moving partitions is risky and may result in data loss. Always backup data before attempting to move partitions.
Copying Partitions
#### Within Same Device
`bash
sudo parted /dev/sdb cp 1 2
`
#### Between Devices
`bash
sudo parted /dev/sdb cp /dev/sda 1 2
`
Rescue Operations
Attempt to rescue a partition that may have been accidentally deleted:
`bash
sudo parted /dev/sdb rescue 10GiB 20GiB
`
Practical Examples
Example 1: Setting Up a New Disk for Linux Installation
This example demonstrates setting up a 500GB disk with GPT partition table for a typical Linux installation:
`bash
Step 1: Create GPT partition table
sudo parted -s /dev/sdb mklabel gptStep 2: Create EFI System Partition (512MB)
sudo parted -s /dev/sdb mkpart "EFI System" fat32 1MiB 513MiB sudo parted -s /dev/sdb set 1 esp onStep 3: Create boot partition (1GB)
sudo parted -s /dev/sdb mkpart "Boot" ext4 513MiB 1537MiB sudo parted -s /dev/sdb set 2 boot onStep 4: Create root partition (remaining space)
sudo parted -s /dev/sdb mkpart "Root" ext4 1537MiB 100%Step 5: Verify the partition layout
sudo parted /dev/sdb print`Example 2: Converting MBR to GPT
Warning: This operation will destroy all data on the disk. Backup all important data first.
`bash
Step 1: Backup partition information
sudo parted /dev/sdb print > partition_backup.txtStep 2: Remove all partitions
sudo parted -s /dev/sdb rm 4 # Remove partitions in reverse order sudo parted -s /dev/sdb rm 3 sudo parted -s /dev/sdb rm 2 sudo parted -s /dev/sdb rm 1Step 3: Create new GPT partition table
sudo parted -s /dev/sdb mklabel gptStep 4: Recreate partitions as needed
sudo parted -s /dev/sdb mkpart primary ext4 1MiB 100%`Example 3: Creating Multiple Partitions with Different File Systems
`bash
Create partition table
sudo parted -s /dev/sdb mklabel gptCreate Windows-compatible partition (NTFS)
sudo parted -s /dev/sdb mkpart "Windows" ntfs 1MiB 100GiBCreate Linux partition (ext4)
sudo parted -s /dev/sdb mkpart "Linux" ext4 100GiB 200GiBCreate shared partition (FAT32)
sudo parted -s /dev/sdb mkpart "Shared" fat32 200GiB 250GiBCreate swap partition
sudo parted -s /dev/sdb mkpart "Swap" linux-swap 250GiB 258GiB sudo parted -s /dev/sdb set 4 swap onUse remaining space for data
sudo parted -s /dev/sdb mkpart "Data" ext4 258GiB 100%`Example 4: Scripted Partition Setup
Create a script for automated partition setup:
`bash
#!/bin/bash
partition_setup.sh
DEVICE="/dev/sdb" SCRIPT_MODE="-s"
echo "Setting up partitions on $DEVICE"
Safety check
if [ ! -b "$DEVICE" ]; then echo "Error: Device $DEVICE does not exist" exit 1 fiCreate partition table
parted $SCRIPT_MODE $DEVICE mklabel gptCreate partitions
parted $SCRIPT_MODE $DEVICE mkpart "EFI" fat32 1MiB 513MiB parted $SCRIPT_MODE $DEVICE mkpart "Boot" ext4 513MiB 1537MiB parted $SCRIPT_MODE $DEVICE mkpart "Root" ext4 1537MiB 50GiB parted $SCRIPT_MODE $DEVICE mkpart "Home" ext4 50GiB 100%Set flags
parted $SCRIPT_MODE $DEVICE set 1 esp on parted $SCRIPT_MODE $DEVICE set 2 boot onDisplay result
parted $DEVICE printecho "Partition setup complete"
`
Best Practices and Safety Considerations
Pre-Partitioning Checklist
| Task | Description | Command Example |
|------|-------------|-----------------|
| Backup Data | Create full backup of important data | rsync -av /source/ /backup/ |
| Unmount Partitions | Ensure target partitions are not mounted | sudo umount /dev/sdb1 |
| Check Disk Health | Verify disk is healthy before partitioning | sudo smartctl -a /dev/sdb |
| Document Current Layout | Save current partition information | sudo parted -l > current_layout.txt |
| Prepare Recovery Media | Have bootable recovery media ready | Create live USB/CD |
Safety Guidelines
#### Always Use Script Mode for Automation
`bash
Good: Script mode prevents accidental confirmations
sudo parted -s /dev/sdb mkpart primary ext4 1MiB 100%Risky: Interactive mode in scripts
sudo parted /dev/sdb mkpart primary ext4 1MiB 100%`#### Verify Device Names
`bash
List all devices before operating
sudo parted -lDouble-check device name
ls -l /dev/disk/by-id/`#### Use Appropriate Alignment
`bash
For SSDs and modern drives
sudo parted -a optimal /dev/sdb mkpart primary ext4 1MiB 100%For maximum compatibility
sudo parted -a minimal /dev/sdb mkpart primary ext4 1MiB 100%`Common Mistakes to Avoid
| Mistake | Consequence | Prevention | |---------|-------------|------------| | Wrong device selection | Data loss on wrong disk | Always verify device names | | Operating on mounted partitions | File system corruption | Unmount before partitioning | | No backup before changes | Permanent data loss | Always backup critical data | | Ignoring alignment | Poor performance | Use optimal alignment | | Scripting without -s flag | Hanging scripts | Always use script mode |
Troubleshooting Common Issues
Partition Table Errors
#### Error: "Invalid partition table"
`bash
Check disk for errors
sudo parted /dev/sdb printIf corrupted, recreate partition table
sudo parted -s /dev/sdb mklabel gpt`#### Error: "Partition outside the disk" This occurs when partition boundaries exceed disk capacity:
`bash
Check actual disk size
sudo parted /dev/sdb printRecreate partition with correct size
sudo parted -s /dev/sdb rm 1 sudo parted -s /dev/sdb mkpart primary ext4 1MiB 100%`Device Busy Errors
#### Error: "Partition is being used"
`bash
Find processes using the partition
sudo lsof /dev/sdb1 sudo fuser -v /dev/sdb1Kill processes if safe to do so
sudo fuser -k /dev/sdb1Unmount the partition
sudo umount /dev/sdb1`Alignment Warnings
#### Warning: "Partition not aligned optimally"
`bash
Check current alignment
sudo parted /dev/sdb align-check optimal 1Recreate with proper alignment
sudo parted -a optimal /dev/sdb mkpart primary ext4 1MiB 100%`Recovery Scenarios
#### Accidentally Deleted Partition Table
`bash
Try to rescue the partition
sudo parted /dev/sdb rescue 0% 100%Use testdisk for advanced recovery
sudo testdisk /dev/sdb`#### Partition Won't Boot After Changes
`bash
Reinstall bootloader (for GRUB)
sudo grub-install /dev/sdb sudo update-grubFor UEFI systems
sudo efibootmgr -c -d /dev/sdb -p 1 -L "Linux" -l '\EFI\grub\grubx64.efi'`Comparison with Other Tools
Parted vs fdisk
| Feature | Parted | fdisk | |---------|--------|-------| | GPT Support | Full support | Limited support | | Real-time Changes | Yes | No (requires write command) | | Scripting | Excellent | Good | | Resize Support | Yes | No | | User Interface | Command-line | Interactive menu | | Learning Curve | Moderate | Easy |
Parted vs gdisk
| Feature | Parted | gdisk | |---------|--------|-------| | GPT Focus | Multi-format | GPT-specific | | MBR Support | Yes | Limited | | Conversion | MBR to GPT | MBR to GPT | | Recovery Features | Basic | Advanced | | Scripting | Better | Limited |
Command Equivalents
#### Creating Partitions
fdisk approach:
`bash
sudo fdisk /dev/sdb
Interactive commands: n, p, 1, enter, enter, w
`parted approach:
`bash
sudo parted -s /dev/sdb mkpart primary ext4 1MiB 100%
`
#### Listing Partitions
fdisk:
`bash
sudo fdisk -l /dev/sdb
`
parted:
`bash
sudo parted /dev/sdb print
`
When to Use Each Tool
#### Use Parted When: - Working with GPT partitions - Need to resize partitions - Automating partition management - Dealing with large disks (>2TB) - Need real-time partition changes
#### Use fdisk When: - Working primarily with MBR - Prefer interactive menu interface - Need maximum compatibility - Working on older systems
#### Use gdisk When: - Exclusively working with GPT - Need advanced GPT features - Recovering corrupted GPT headers - Converting complex MBR layouts
Integration with File System Tools
After creating partitions with parted, you typically need to create file systems:
`bash
Create ext4 file system
sudo mkfs.ext4 /dev/sdb1Create FAT32 file system
sudo mkfs.fat -F32 /dev/sdb2Create NTFS file system
sudo mkfs.ntfs -f /dev/sdb3Create XFS file system
sudo mkfs.xfs /dev/sdb4`Monitoring and Maintenance
#### Regular Health Checks
`bash
Check partition alignment
sudo parted /dev/sdb align-check optimal 1Verify partition table integrity
sudo parted /dev/sdb printCheck file system health
sudo fsck /dev/sdb1`#### Performance Optimization
`bash
Enable TRIM for SSDs (if supported)
sudo fstrim -v /Check I/O scheduler
cat /sys/block/sdb/queue/schedulerOptimize mount options in /etc/fstab
For SSDs: add noatime,discard options
`Parted is a powerful and versatile tool for disk partitioning that provides comprehensive functionality for modern storage management needs. Its support for multiple partition table formats, scripting capabilities, and real-time operation make it an essential tool for system administrators and power users. While it requires careful handling due to its immediate effect on disk structures, following proper safety procedures and best practices ensures reliable and efficient partition management.