Creating Partitions with fdisk: Complete Guide
Table of Contents
1. [Introduction](#introduction) 2. [Understanding Partitions](#understanding-partitions) 3. [fdisk Overview](#fdisk-overview) 4. [Prerequisites](#prerequisites) 5. [Basic fdisk Commands](#basic-fdisk-commands) 6. [Partition Creation Process](#partition-creation-process) 7. [Practical Examples](#practical-examples) 8. [Advanced Operations](#advanced-operations) 9. [Best Practices](#best-practices) 10. [Troubleshooting](#troubleshooting)Introduction
The fdisk command is a fundamental disk partitioning utility in Linux systems that allows users to create, delete, modify, and manage disk partitions. This command-line tool provides direct access to disk partition tables and is essential for system administration tasks involving storage management.
Partitioning is the process of dividing a physical disk into logical sections called partitions, each of which can be treated as a separate storage unit. This allows for better organization of data, installation of multiple operating systems, and improved system performance through strategic data placement.
Understanding Partitions
Partition Types
| Partition Type | Description | Maximum Count | Usage | |----------------|-------------|---------------|--------| | Primary | Main partitions that can contain bootable operating systems | 4 per disk | Operating systems, boot partitions | | Extended | Container partition that can hold multiple logical partitions | 1 per disk | Used to overcome 4-partition limit | | Logical | Partitions created within an extended partition | Unlimited | Data storage, additional file systems |
Partition Table Types
| Type | Description | Maximum Disk Size | Maximum Partitions | Compatibility | |------|-------------|-------------------|-------------------|---------------| | MBR (Master Boot Record) | Traditional partition table format | 2 TB | 4 primary | Legacy BIOS systems | | GPT (GUID Partition Table) | Modern partition table format | 9.4 ZB | 128 partitions | UEFI systems |
fdisk Overview
The fdisk utility operates in two primary modes:
- Interactive mode: Provides a command prompt for partition management
- Non-interactive mode: Executes specific operations through command-line parameters
Key Features
- Create, delete, and modify partitions - Display partition information - Change partition types - Set bootable flags - Work with both MBR and GPT partition tables - Verify partition table integrity
Prerequisites
Before using fdisk, ensure you have:
1. Root privileges: Most fdisk operations require administrative access 2. Backup important data: Partitioning can result in data loss 3. Identify target disk: Know the device name of the disk to partition 4. Unmount partitions: Unmount any mounted partitions on the target disk
Identifying Disks and Partitions
`bash
List all block devices
lsblkDisplay disk information
fdisk -lShow mounted file systems
df -hList partition information
cat /proc/partitions`Basic fdisk Commands
Starting fdisk
`bash
Start fdisk for a specific disk
fdisk /dev/sdaDisplay help information
fdisk --helpList all disks and partitions
fdisk -lWork with a specific disk in script mode
fdisk -u /dev/sda`Interactive Mode Commands
| Command | Description | Usage Example |
|---------|-------------|---------------|
| p | Print partition table | Shows current partitions |
| n | Create new partition | Add primary, extended, or logical partition |
| d | Delete partition | Remove existing partition |
| t | Change partition type | Modify file system type |
| a | Toggle bootable flag | Set partition as bootable |
| w | Write changes to disk | Save modifications |
| q | Quit without saving | Exit without changes |
| m | Display help menu | Show available commands |
| l | List known partition types | Display file system codes |
Advanced Commands
| Command | Description | Notes |
|---------|-------------|-------|
| u | Change display units | Toggle between sectors and cylinders |
| x | Enter expert mode | Access advanced functions |
| v | Verify partition table | Check for errors |
| i | Print information about partition | Detailed partition data |
| F | List unpartitioned free space | Show available space |
Partition Creation Process
Step-by-Step Partition Creation
#### 1. Launch fdisk
`bash
sudo fdisk /dev/sda
`
#### 2. Display Current Partition Table
`
Command (m for help): p
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors Disk model: Virtual Disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x12345678
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 209715199 207616000 99G 8e Linux LVM
`
#### 3. Create New Partition
`
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical drives)
Select (default p): p
Partition number (3-4, default 3): 3
First sector (2099200-209715199, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-209715199, default 209715199): +10G
Created a new partition 3 of type 'Linux' and of size 10 GiB.
`
#### 4. Verify New Partition
`
Command (m for help): p
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 4196351 2097152 1G 8e Linux LVM
/dev/sda3 4196352 25167871 20971520 10G 83 Linux
`
#### 5. Save Changes
`
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
`
Practical Examples
Example 1: Creating Multiple Partitions on a New Disk
Scenario: Partitioning a new 50GB disk with separate partitions for boot, root, and home directories.
`bash
Start fdisk
sudo fdisk /dev/sdbCreate boot partition (1GB)
Command (m for help): n Select (default p): p Partition number (1-4, default 1): 1 First sector: [Enter for default] Last sector: +1GCreate root partition (20GB)
Command (m for help): n Select (default p): p Partition number (2-4, default 2): 2 First sector: [Enter for default] Last sector: +20GCreate home partition (remaining space)
Command (m for help): n Select (default p): p Partition number (3-4, default 3): 3 First sector: [Enter for default] Last sector: [Enter for default]Set boot partition as bootable
Command (m for help): a Partition number (1-3, default 3): 1Write changes
Command (m for help): w`Example 2: Creating Extended and Logical Partitions
`bash
sudo fdisk /dev/sdc
Create primary partition
Command (m for help): n Select (default p): p Partition number: 1 First sector: [Enter] Last sector: +10GCreate extended partition
Command (m for help): n Select (default p): e Partition number: 2 First sector: [Enter] Last sector: [Enter for remaining space]Create logical partition within extended
Command (m for help): n Select (default p): l First sector: [Enter] Last sector: +5GCreate another logical partition
Command (m for help): n Select (default p): l First sector: [Enter] Last sector: +5G`Example 3: Changing Partition Types
`bash
Display current partition table
Command (m for help): pList available partition types
Command (m for help): lChange partition type
Command (m for help): t Partition number: 2 Hex code (type L to list all codes): 82 # Linux swapVerify change
Command (m for help): p`Advanced Operations
Working with GPT Partition Tables
For disks larger than 2TB or systems requiring more than 4 partitions, use GPT:
`bash
Create GPT partition table
sudo fdisk /dev/sddCommand (m for help): g
Created a new GPT disklabel (GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
`
Partition Alignment
Modern storage devices require proper partition alignment for optimal performance:
`bash
Check current alignment
sudo fdisk -l /dev/sdaCreate aligned partitions (fdisk handles this automatically in recent versions)
Command (m for help): nUse default values for optimal alignment
`Script Mode Operations
For automated partition creation:
`bash
Create script file
cat << EOF > partition_script.txt n p 1+10G n p 2
+20G w EOF
Execute script
sudo fdisk /dev/sde < partition_script.txt`Partition Type Codes
Common MBR Partition Types
| Code | Type | Description | |------|------|-------------| | 83 | Linux | Standard Linux file system | | 82 | Linux swap | Swap partition | | 8e | Linux LVM | Logical Volume Manager | | 7 | HPFS/NTFS/exFAT | Windows file systems | | b | W95 FAT32 | FAT32 file system | | c | W95 FAT32 (LBA) | FAT32 with LBA | | ef | EFI (FAT-12/16/32) | EFI system partition | | fd | Linux raid autodetect | Software RAID |
GPT Partition Types
| GUID | Type | Description | |------|------|-------------| | 0FC63DAF-8483-4772-8E79-3D69D8477DE4 | Linux filesystem | Standard Linux partition | | 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F | Linux swap | Linux swap partition | | E3C9E316-0B5C-4DB8-817D-F92DF00215AE | Microsoft Reserved | Windows reserved partition | | EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 | Microsoft basic data | Windows data partition |
Best Practices
Planning Partition Layout
1. Boot Partition: Create separate boot partition (100MB-1GB) for complex setups 2. Root Partition: Allocate sufficient space for system files (15-30GB minimum) 3. Home Partition: Separate user data for easier system upgrades 4. Swap Partition: Size according to RAM amount and usage requirements
Size Recommendations
| Partition | Minimum Size | Recommended Size | Notes | |-----------|--------------|------------------|--------| | /boot | 100MB | 500MB-1GB | Kernel and bootloader files | | / (root) | 15GB | 25-50GB | System files and applications | | /home | 5GB | 50GB+ | User data and documents | | swap | 512MB | 1-2x RAM | Virtual memory |
Safety Guidelines
1. Always backup data before partitioning 2. Double-check device names to avoid wrong disk modification 3. Verify partition table before writing changes 4. Test partition layout on virtual machines first 5. Document partition scheme for future reference
Performance Considerations
1. Align partitions to storage device boundaries 2. Place frequently accessed data on faster storage areas 3. Separate I/O intensive partitions across different drives 4. Consider SSD-specific optimizations for solid-state drives
Troubleshooting
Common Issues and Solutions
#### Issue: "Device or resource busy" Error
`bash
Check what's using the device
lsof /dev/sda1 fuser -v /dev/sda1Unmount the partition
sudo umount /dev/sda1Stop any processes using the device
sudo fuser -k /dev/sda1`#### Issue: Partition Table Corruption
`bash
Backup current partition table
sudo sfdisk -d /dev/sda > partition_backup.txtRestore partition table
sudo sfdisk /dev/sda < partition_backup.txtUse testdisk for recovery
sudo testdisk /dev/sda`#### Issue: Cannot Create More Than 4 Partitions
Solution: Use extended partition to create logical partitions
`bash
Delete one primary partition
Command (m for help): d Partition number: 4Create extended partition
Command (m for help): n Select (default p): e Partition number: 4Create logical partitions within extended
Command (m for help): n Select (default p): l`Verification Commands
`bash
Check partition table integrity
sudo fdisk -l /dev/sdaVerify file system
sudo fsck /dev/sda1Check partition alignment
sudo fdisk -l -u /dev/sdaDisplay detailed partition information
sudo parted /dev/sda print`Recovery Procedures
#### Recovering Deleted Partition Table
`bash
Use testdisk for interactive recovery
sudo testdiskUse photorec for file recovery
sudo photorecRestore from backup
sudo dd if=mbr_backup.img of=/dev/sda bs=512 count=1`Post-Partitioning Steps
After creating partitions with fdisk, additional steps are required:
1. Create File Systems
`bash
Format ext4 file system
sudo mkfs.ext4 /dev/sda1Format swap partition
sudo mkswap /dev/sda2Format FAT32 file system
sudo mkfs.fat -F32 /dev/sda3`2. Mount Partitions
`bash
Create mount point
sudo mkdir /mnt/newpartitionMount partition
sudo mount /dev/sda1 /mnt/newpartitionAdd to fstab for permanent mounting
echo "/dev/sda1 /mnt/newpartition ext4 defaults 0 2" | sudo tee -a /etc/fstab`3. Verify Setup
`bash
Check mounted file systems
df -hVerify partition table
sudo fdisk -lTest write access
sudo touch /mnt/newpartition/testfile`Conclusion
The fdisk utility is an essential tool for disk partition management in Linux systems. Understanding its commands, options, and best practices enables system administrators to effectively organize storage, optimize performance, and maintain system reliability. Always exercise caution when modifying partition tables, maintain backups, and test procedures in safe environments before applying them to production systems.
Proper partition planning and implementation using fdisk provides the foundation for stable, organized, and efficient storage management in Linux environments. Regular practice with these commands and procedures will develop the expertise needed for confident disk management operations.