How to use parted for disk partitioning
How to Use Parted for Disk Partitioning
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites and Requirements](#prerequisites-and-requirements)
3. [Understanding Parted Basics](#understanding-parted-basics)
4. [Installation and Setup](#installation-and-setup)
5. [Basic Parted Operations](#basic-parted-operations)
6. [Advanced Partitioning Techniques](#advanced-partitioning-techniques)
7. [Practical Examples and Use Cases](#practical-examples-and-use-cases)
8. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting)
9. [Best Practices and Professional Tips](#best-practices-and-professional-tips)
10. [Conclusion](#conclusion)
Introduction
GNU Parted is a powerful command-line disk partitioning utility that allows system administrators and advanced users to create, resize, move, and manage disk partitions on Linux and Unix-like systems. Unlike traditional tools like fdisk, parted supports both MBR (Master Boot Record) and GPT (GUID Partition Table) partition schemes, making it an essential tool for modern disk management.
This comprehensive guide will teach you everything you need to know about using parted effectively, from basic partition creation to advanced disk management scenarios. Whether you're setting up a new server, managing storage arrays, or preparing disks for virtualization, mastering parted will significantly enhance your system administration capabilities.
By the end of this article, you'll understand how to safely manipulate disk partitions, avoid common pitfalls, and implement best practices that protect your data while maximizing storage efficiency.
Prerequisites and Requirements
System Requirements
Before working with parted, ensure your system meets these requirements:
- Operating System: Linux distribution with kernel 2.6 or later
- User Privileges: Root access or sudo privileges
- Available Storage: Target disk with unallocated space
- Backup Strategy: Current backups of important data
Essential Knowledge
You should have basic familiarity with:
- Linux command-line interface
- File system concepts (ext4, xfs, ntfs, etc.)
- Disk naming conventions (/dev/sda, /dev/nvme0n1, etc.)
- Partition table types (MBR vs GPT)
Safety Considerations
WARNING: Disk partitioning operations can result in complete data loss. Always:
- Create complete system backups before proceeding
- Verify disk identities using multiple methods
- Test commands on non-production systems first
- Understand the implications of each operation
Understanding Parted Basics
Partition Table Types
Parted supports two primary partition table formats:
Master Boot Record (MBR)
- Legacy format supporting up to 4 primary partitions
- Maximum disk size: 2TB
- Compatible with older BIOS systems
- Uses extended partitions for more than 4 partitions
GUID Partition Table (GPT)
- Modern format supporting up to 128 partitions
- Maximum disk size: 9.4ZB (theoretical)
- Required for UEFI boot systems
- Includes redundant partition table for reliability
Parted Operational Modes
Parted operates in two distinct modes:
Interactive Mode
- Launches an interactive shell for multiple operations
- Ideal for complex partitioning tasks
- Provides command history and tab completion
- Allows verification before committing changes
Command-line Mode
- Executes single commands directly
- Perfect for scripting and automation
- Immediate execution without confirmation
- Suitable for simple, well-understood operations
Installation and Setup
Installing Parted
Most Linux distributions include parted by default. If not installed, use your distribution's package manager:
Ubuntu/Debian Systems
```bash
sudo apt update
sudo apt install parted
```
Red Hat/CentOS/Fedora Systems
```bash
For RHEL/CentOS 8+ and Fedora
sudo dnf install parted
For older RHEL/CentOS versions
sudo yum install parted
```
Arch Linux
```bash
sudo pacman -S parted
```
Verifying Installation
Confirm parted installation and check version:
```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 .
```
Identifying Available Disks
Before partitioning, identify available storage devices:
```bash
List all block devices
lsblk
Show detailed disk information
sudo fdisk -l
Display partition information
sudo parted -l
```
Example output from `lsblk`:
```
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part /
sdb 8:16 0 10G 0 disk
```
Basic Parted Operations
Launching Parted Interactive Mode
Start parted in interactive mode by specifying a target disk:
```bash
sudo parted /dev/sdb
```
This launches the parted prompt:
```
GNU Parted 3.4
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
```
Essential Parted Commands
Getting Help
```bash
(parted) help
```
For specific command help:
```bash
(parted) help print
```
Displaying Current Partition Table
```bash
(parted) print
```
Example output:
```
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
```
Creating a Partition Table
For new disks, create a partition table first:
```bash
Create GPT partition table (recommended for new systems)
(parted) mklabel gpt
Create MBR partition table (for legacy compatibility)
(parted) mklabel msdos
```
WARNING: Creating a new partition table destroys all existing data on the disk.
Creating Partitions
Basic partition creation syntax:
```bash
(parted) mkpart [partition-type] [file-system-type] [start] [end]
```
Examples:
```bash
Create a primary partition using entire disk
(parted) mkpart primary ext4 0% 100%
Create a partition from 1GB to 5GB
(parted) mkpart primary ext4 1GB 5GB
Create a partition with specific sector alignment
(parted) mkpart primary ext4 2048s 4194303s
```
Deleting Partitions
Remove partitions by number:
```bash
(parted) rm 1
```
Resizing Partitions
Resize existing partitions:
```bash
(parted) resizepart 1 8GB
```
Note: File system resizing requires additional steps using file system-specific tools.
Setting Partition Flags
Common partition flags:
```bash
Set boot flag (for bootable partitions)
(parted) set 1 boot on
Set LVM flag
(parted) set 1 lvm on
Set RAID flag
(parted) set 1 raid on
```
Exiting Parted
Exit the interactive mode:
```bash
(parted) quit
```
Advanced Partitioning Techniques
Working with Large Disks
For disks larger than 2TB, GPT is mandatory:
```bash
sudo parted /dev/sdc
(parted) mklabel gpt
(parted) mkpart primary ext4 0% 100%
(parted) print
```
Creating Multiple Partitions
Example: Creating a multi-partition layout for a server:
```bash
sudo parted /dev/sdd
(parted) mklabel gpt
Boot partition (1GB)
(parted) mkpart boot ext4 1MB 1GB
(parted) set 1 boot on
Root partition (20GB)
(parted) mkpart root ext4 1GB 21GB
Home partition (remaining space)
(parted) mkpart home ext4 21GB 100%
(parted) print
(parted) quit
```
Precise Alignment for Performance
Ensure optimal performance with proper alignment:
```bash
Check current alignment
(parted) align-check optimal 1
Create partition with optimal alignment
(parted) mkpart primary ext4 2048s -1s
```
Working with Different File Systems
Parted supports various file system types:
```bash
Linux file systems
(parted) mkpart primary ext4 1GB 5GB
(parted) mkpart primary xfs 5GB 10GB
(parted) mkpart primary btrfs 10GB 15GB
Windows file systems
(parted) mkpart primary ntfs 15GB 20GB
(parted) mkpart primary fat32 20GB 21GB
```
Command-line Mode Operations
Execute parted commands directly without interactive mode:
```bash
Create GPT partition table
sudo parted /dev/sde mklabel gpt
Create partition in one command
sudo parted /dev/sde mkpart primary ext4 0% 100%
Set partition flags
sudo parted /dev/sde set 1 boot on
Display partition information
sudo parted /dev/sde print
```
Practical Examples and Use Cases
Example 1: Setting Up a Web Server Disk Layout
Scenario: Preparing a 100GB disk for a web server with separate partitions for system, web content, and logs.
```bash
sudo parted /dev/sdf
(parted) mklabel gpt
System partition (30GB)
(parted) mkpart system ext4 1MB 30GB
Web content partition (50GB)
(parted) mkpart webroot ext4 30GB 80GB
Log partition (remaining space)
(parted) mkpart logs ext4 80GB 100%
(parted) print
(parted) quit
Format the partitions
sudo mkfs.ext4 /dev/sdf1
sudo mkfs.ext4 /dev/sdf2
sudo mkfs.ext4 /dev/sdf3
```
Example 2: Preparing Disk for LVM
Setting up a disk for Logical Volume Management:
```bash
sudo parted /dev/sdg
(parted) mklabel gpt
(parted) mkpart primary ext4 0% 100%
(parted) set 1 lvm on
(parted) print
(parted) quit
Initialize as LVM physical volume
sudo pvcreate /dev/sdg1
```
Example 3: Creating a Dual-Boot Setup
Preparing a disk for Windows and Linux dual-boot:
```bash
sudo parted /dev/sdh
(parted) mklabel gpt
EFI System Partition (512MB)
(parted) mkpart ESP fat32 1MB 513MB
(parted) set 1 esp on
Windows partition (50GB)
(parted) mkpart windows ntfs 513MB 50GB
Linux root partition (30GB)
(parted) mkpart linux-root ext4 50GB 80GB
Linux home partition (remaining)
(parted) mkpart linux-home ext4 80GB 100%
(parted) print
(parted) quit
```
Example 4: Expanding an Existing Partition
Extending a partition to use additional disk space:
```bash
First, extend the disk at hardware/VM level
Then extend the partition
sudo parted /dev/sdi
(parted) print
(parted) resizepart 1 100%
(parted) quit
Resize the file system
sudo resize2fs /dev/sdi1 # for ext4
or
sudo xfs_growfs /mount/point # for XFS
```
Example 5: Scripted Partition Creation
Automated partitioning script for deployment:
```bash
#!/bin/bash
DISK="/dev/sdj"
Create partition table and partitions
sudo parted $DISK mklabel gpt
sudo parted $DISK mkpart boot ext4 1MB 1GB
sudo parted $DISK mkpart root ext4 1GB 21GB
sudo parted $DISK mkpart home ext4 21GB 100%
sudo parted $DISK set 1 boot on
Format partitions
sudo mkfs.ext4 ${DISK}1
sudo mkfs.ext4 ${DISK}2
sudo mkfs.ext4 ${DISK}3
echo "Partitioning complete for $DISK"
sudo parted $DISK print
```
Common Issues and Troubleshooting
Issue 1: "Device is Busy" Error
Problem: Cannot modify partitions because the device is in use.
Solution:
```bash
Check what's using the device
sudo fuser -v /dev/sdb1
Unmount the partition
sudo umount /dev/sdb1
Stop any swap usage
sudo swapoff /dev/sdb1
Check for LVM usage
sudo lvdisplay
sudo vgdisplay
```
Issue 2: Partition Alignment Warnings
Problem: Parted warns about suboptimal partition alignment.
Solution:
```bash
Check alignment
(parted) align-check optimal 1
If misaligned, recreate with proper alignment
(parted) rm 1
(parted) mkpart primary ext4 2048s -1s
```
Issue 3: "Invalid Partition Table" Error
Problem: Corrupted or unrecognized partition table.
Solutions:
```bash
Try to repair the partition table
sudo parted /dev/sdb print
If repair fails, create new partition table (DATA LOSS!)
sudo parted /dev/sdb mklabel gpt
Use testdisk for data recovery attempts
sudo apt install testdisk
sudo testdisk
```
Issue 4: Cannot Create More Than 4 Partitions on MBR
Problem: MBR limitation prevents creating additional partitions.
Solutions:
```bash
Option 1: Convert to GPT (may cause boot issues)
sudo parted /dev/sdb mklabel gpt
Option 2: Use extended partitions
(parted) mkpart extended 10GB 100%
(parted) mkpart logical ext4 10GB 20GB
(parted) mkpart logical ext4 20GB 30GB
```
Issue 5: File System Doesn't Fill Partition After Resize
Problem: Partition expanded but file system remains original size.
Solution:
```bash
For ext2/ext3/ext4
sudo resize2fs /dev/sdb1
For XFS (must be mounted)
sudo xfs_growfs /mount/point
For NTFS
sudo ntfsresize /dev/sdb1
```
Issue 6: Boot Issues After Partitioning
Problem: System won't boot after partition changes.
Solutions:
```bash
Update GRUB configuration
sudo update-grub
Reinstall GRUB bootloader
sudo grub-install /dev/sdb
Check /etc/fstab for correct UUIDs
sudo blkid
sudo nano /etc/fstab
```
Issue 7: Permission Denied Errors
Problem: Cannot access parted or modify partitions.
Solution:
```bash
Ensure running as root
sudo parted /dev/sdb
Check device permissions
ls -l /dev/sdb
Verify disk isn't read-only
echo 0 | sudo tee /sys/block/sdb/ro
```
Best Practices and Professional Tips
Data Safety Practices
1. Always Backup First
```bash
# Create full disk image
sudo dd if=/dev/sdb of=/backup/sdb-backup.img bs=4M status=progress
# Create partition table backup
sudo sfdisk -d /dev/sdb > /backup/sdb-partition-table.txt
```
2. Verify Disk Identity
```bash
# Multiple verification methods
sudo parted -l
lsblk -f
sudo blkid
ls -la /dev/disk/by-id/
```
3. Test in Non-Production Environment
- Use virtual machines for testing
- Practice on spare hardware
- Document successful procedures
Performance Optimization
1. Proper Alignment
```bash
# Always check alignment after creation
(parted) align-check optimal 1
# Use sector-based addressing for precision
(parted) mkpart primary ext4 2048s -1s
```
2. Choose Appropriate Partition Sizes
- Leave 10-20% free space in file systems
- Consider future growth requirements
- Use LVM for flexible storage management
3. File System Selection
- ext4: General purpose, stable
- XFS: Large files, high performance
- Btrfs: Advanced features, snapshots
- ZFS: Enterprise features, data integrity
Automation and Scripting
1. Scripted Deployments
```bash
#!/bin/bash
set -e # Exit on any error
DISK="$1"
if [ -z "$DISK" ]; then
echo "Usage: $0 /dev/sdX"
exit 1
fi
# Verify disk exists
if [ ! -b "$DISK" ]; then
echo "Error: $DISK is not a block device"
exit 1
fi
# Create partitions
sudo parted $DISK mklabel gpt
sudo parted $DISK mkpart boot ext4 1MB 1GB
sudo parted $DISK mkpart root ext4 1GB 100%
sudo parted $DISK set 1 boot on
```
2. Configuration Management
- Use tools like Ansible, Puppet, or Chef
- Version control your partitioning scripts
- Test thoroughly before production deployment
Monitoring and Maintenance
1. Regular Health Checks
```bash
# Check file system integrity
sudo fsck -n /dev/sdb1
# Monitor disk usage
df -h
# Check for bad sectors
sudo badblocks -v /dev/sdb
```
2. Capacity Planning
```bash
# Monitor growth trends
du -sh /var/log/*
# Set up alerts for disk usage
# Use tools like Nagios, Zabbix, or custom scripts
```
Security Considerations
1. Encryption Setup
```bash
# Create encrypted partition
sudo cryptsetup luksFormat /dev/sdb1
sudo cryptsetup luksOpen /dev/sdb1 encrypted_disk
sudo mkfs.ext4 /dev/mapper/encrypted_disk
```
2. Secure Deletion
```bash
# Securely wipe partition before reuse
sudo shred -vfz -n 3 /dev/sdb1
# Or use dd with random data
sudo dd if=/dev/urandom of=/dev/sdb1 bs=4M status=progress
```
Documentation and Change Management
1. Document All Changes
- Record partition layouts
- Note file system types and mount points
- Keep configuration backups
- Maintain change logs
2. Version Control
```bash
# Save partition information
sudo parted -l > partition-layout-$(date +%Y%m%d).txt
sudo sfdisk -d /dev/sdb > sdb-partition-$(date +%Y%m%d).bak
```
Conclusion
Mastering parted for disk partitioning is an essential skill for system administrators and advanced Linux users. This comprehensive guide has covered everything from basic partition creation to advanced techniques and troubleshooting scenarios.
Key takeaways from this guide include:
- Safety First: Always backup data and verify disk identities before making changes
- Choose the Right Tools: Use GPT for modern systems and large disks, MBR only when necessary for legacy compatibility
- Plan Ahead: Consider future growth, performance requirements, and maintenance needs
- Automate Wisely: Script repetitive tasks but always include proper error checking and validation
- Monitor Continuously: Regular health checks and capacity planning prevent future issues
The parted utility's flexibility and power make it indispensable for managing storage in modern Linux environments. Whether you're setting up a simple desktop system or managing complex server storage arrays, the techniques and best practices outlined in this guide will help you work efficiently and safely.
Remember that disk partitioning is a critical system administration task that requires careful planning and execution. Continue practicing these techniques in safe environments, stay updated with the latest best practices, and always prioritize data safety in your operations.
For further learning, consider exploring related topics such as Logical Volume Management (LVM), software RAID configuration, and advanced file system features. These complementary technologies work alongside parted to provide comprehensive storage management solutions for enterprise environments.