How to (g)partition disks → fdisk /dev/sdX; gdisk /dev/nvme0n1

How to Partition Disks Using fdisk and gdisk Commands Disk partitioning is a fundamental skill for Linux system administrators, developers, and power users. Whether you're setting up a new system, adding storage, or reorganizing existing drives, understanding how to properly partition disks using `fdisk` and `gdisk` is essential. This comprehensive guide will walk you through the process of partitioning both traditional SATA drives and modern NVMe storage devices. Table of Contents 1. [Introduction to Disk Partitioning](#introduction-to-disk-partitioning) 2. [Prerequisites and Requirements](#prerequisites-and-requirements) 3. [Understanding MBR vs GPT](#understanding-mbr-vs-gpt) 4. [Using fdisk for MBR Partitioning](#using-fdisk-for-mbr-partitioning) 5. [Using gdisk for GPT Partitioning](#using-gdisk-for-gpt-partitioning) 6. [Practical Examples and Use Cases](#practical-examples-and-use-cases) 7. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting) 8. [Best Practices and Professional Tips](#best-practices-and-professional-tips) 9. [Conclusion](#conclusion) Introduction to Disk Partitioning Disk partitioning is the process of dividing a physical storage device into logical sections called partitions. Each partition functions as a separate storage unit that can be formatted with different file systems, used for different purposes, or even contain different operating systems. The two primary tools for partitioning in Linux are `fdisk` for Master Boot Record (MBR) partitions and `gdisk` for GUID Partition Table (GPT) partitions. Understanding when and how to use these tools is crucial for: - Installing operating systems - Creating separate storage areas for different purposes - Setting up dual-boot configurations - Managing server storage - Preparing drives for RAID configurations - Optimizing storage performance Prerequisites and Requirements Before beginning disk partitioning operations, ensure you have: System Requirements - Root or sudo privileges on your Linux system - Access to a terminal or command line interface - Basic understanding of Linux file systems - Knowledge of your system's storage device naming conventions Essential Tools - `fdisk` (usually pre-installed on most Linux distributions) - `gdisk` (may need installation: `sudo apt install gdisk` or `sudo yum install gdisk`) - `lsblk` for viewing block devices - `parted` as an alternative tool (optional) Safety Precautions ⚠️ WARNING: Partitioning operations can result in complete data loss. Always: - Back up important data before proceeding - Double-check device names to avoid partitioning the wrong disk - Test procedures on non-critical systems first - Verify partition tables before making changes permanent Identifying Your Storage Devices Before partitioning, identify your storage devices using these commands: ```bash List all block devices lsblk Show detailed disk information sudo fdisk -l Display partition information cat /proc/partitions ``` Common device naming conventions: - SATA/IDE drives: `/dev/sda`, `/dev/sdb`, `/dev/sdc` - NVMe drives: `/dev/nvme0n1`, `/dev/nvme1n1` - Virtual drives: `/dev/vda`, `/dev/vdb` (in virtual machines) Understanding MBR vs GPT Master Boot Record (MBR) MBR is the traditional partitioning scheme with these characteristics: - Maximum of 4 primary partitions - Support for extended partitions to create more logical partitions - 2TB maximum disk size limitation - 32-bit partition table entries - Compatible with older BIOS systems GUID Partition Table (GPT) GPT is the modern partitioning standard featuring: - Support for up to 128 partitions (typically) - Support for disks larger than 2TB (up to 9.4ZB theoretically) - 64-bit partition table entries - Built-in redundancy and error checking - Required for UEFI boot systems - Backward compatibility with BIOS through protective MBR Choosing Between MBR and GPT Use MBR when: - Working with older systems that require BIOS boot - Disk size is under 2TB - Maximum compatibility is needed with older tools Use GPT when: - Working with modern UEFI systems - Disk size exceeds 2TB - You need more than 4 partitions - Working with NVMe drives - Enhanced reliability is required Using fdisk for MBR Partitioning Basic fdisk Operations The `fdisk` command is the traditional tool for creating and managing MBR partition tables. Here's how to use it effectively: Starting fdisk ```bash Open fdisk for a specific device (replace X with appropriate letter) sudo fdisk /dev/sdX Example for the first SATA drive sudo fdisk /dev/sda ``` Essential fdisk Commands Once inside fdisk, use these single-letter commands: - `m` - Display help menu - `p` - Print partition table - `n` - Create new partition - `d` - Delete partition - `t` - Change partition type - `w` - Write changes and exit - `q` - Quit without saving changes Step-by-Step Partitioning with fdisk Step 1: Examine Current Partition Table ```bash sudo fdisk /dev/sda Command (m for help): p ``` This displays the current partition layout, showing: - Device names - Boot flags - Start and end sectors - Size information - Partition types Step 2: Create a New Partition ```bash Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-41943039, default 2048): [Press Enter] Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G ``` This creates a 10GB primary partition using default alignment. Step 3: Set Partition Type ```bash Command (m for help): t Selected partition 1 Partition type (type L to list all types): 83 ``` Common partition types: - `83` - Linux filesystem - `82` - Linux swap - `8e` - Linux LVM - `fd` - Linux RAID - `0c` - FAT32 LBA Step 4: Verify and Write Changes ```bash Review the partition table Command (m for help): p Write changes to disk Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. ``` Advanced fdisk Techniques Creating Extended Partitions When you need more than 4 partitions with MBR: ```bash Command (m for help): n Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): e Partition number (1-4, default 4): 4 First sector: [Press Enter for default] Last sector: [Press Enter to use remaining space] Now create logical partitions within the extended partition Command (m for help): n All primary partitions are in use Adding logical partition 5 First sector: [Press Enter] Last sector: +5G ``` Alignment Optimization Modern drives benefit from proper partition alignment: ```bash Check current alignment sudo fdisk -l /dev/sda | grep -E "(Units|Sector)" fdisk typically aligns to 2048 sectors (1MB) by default This works well for most modern drives ``` Using gdisk for GPT Partitioning Introduction to gdisk `gdisk` is specifically designed for GPT partition tables and offers more advanced features than fdisk. It's essential for modern systems and large drives. Starting gdisk ```bash Open gdisk for an NVMe drive sudo gdisk /dev/nvme0n1 Or for a SATA drive sudo gdisk /dev/sda ``` Essential gdisk Commands Key commands in gdisk: - `?` - Display help - `p` - Print partition table - `n` - Create new partition - `d` - Delete partition - `t` - Change partition type code - `c` - Change partition name - `i` - Show detailed partition information - `v` - Verify disk - `w` - Write table to disk and exit - `q` - Quit without saving Step-by-Step GPT Partitioning Step 1: Initialize GPT Table (if needed) ```bash sudo gdisk /dev/nvme0n1 Command (? for help): o This option deletes all partitions and creates a new protective MBR and a new GPT. Do you want to proceed? (Y/N): y ``` Step 2: Create EFI System Partition (for UEFI systems) ```bash Command (? for help): n Partition number (1-128, default 1): 1 First sector: [Press Enter for default] Last sector: +512M Current type is 'Linux filesystem' Hex code or GUID: ef00 Changed type of partition to 'EFI System' ``` Step 3: Create Additional Partitions ```bash Create root partition Command (? for help): n Partition number (2-128, default 2): 2 First sector: [Press Enter] Last sector: +50G Hex code or GUID: 8300 # Linux filesystem Create home partition Command (? for help): n Partition number (3-128, default 3): 3 First sector: [Press Enter] Last sector: [Press Enter for remaining space] Hex code or GUID: 8302 # Linux /home ``` Step 4: Set Partition Names ```bash Command (? for help): c Partition number (1-3): 1 Enter name: EFI System Command (? for help): c Partition number (1-3): 2 Enter name: Root Command (? for help): c Partition number (1-3): 3 Enter name: Home ``` Step 5: Verify and Write ```bash Verify the partition table Command (? for help): v No problems found. 34 free sectors (17.0 KiB) available in 1 segments. Print final layout Command (? for help): p Write changes Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y ``` Advanced gdisk Features Working with Partition Type Codes gdisk uses hexadecimal codes for partition types: ```bash List all available type codes Command (? for help): L Common GPT type codes: ef00 - EFI System 8200 - Linux swap 8300 - Linux filesystem 8301 - Linux reserved 8302 - Linux /home 8303 - Linux x86 root (/) 8304 - Linux x86-64 root (/) fd00 - Linux RAID ``` Backup and Restore Operations ```bash Backup GPT to file sudo gdisk /dev/nvme0n1 Command (? for help): b Enter backup filename to save: /home/user/gpt-backup.bin Restore GPT from file Command (? for help): l Enter backup filename to load: /home/user/gpt-backup.bin ``` Practical Examples and Use Cases Example 1: Setting Up a Basic Linux System on NVMe This example creates a typical Linux installation layout on an NVMe drive: ```bash sudo gdisk /dev/nvme0n1 Create EFI System Partition (512MB) Command: n Partition number: 1 First sector: [Enter] Last sector: +512M Hex code: ef00 Create swap partition (8GB) Command: n Partition number: 2 First sector: [Enter] Last sector: +8G Hex code: 8200 Create root partition (50GB) Command: n Partition number: 3 First sector: [Enter] Last sector: +50G Hex code: 8304 Create home partition (remaining space) Command: n Partition number: 4 First sector: [Enter] Last sector: [Enter] Hex code: 8302 Set names Command: c Partition: 1, Name: EFI Command: c Partition: 2, Name: Swap Command: c Partition: 3, Name: Root Command: c Partition: 4, Name: Home Write changes Command: w ``` Example 2: Creating a Data Drive with Multiple Partitions Setting up a secondary drive for data storage: ```bash sudo fdisk /dev/sdb Create first data partition (100GB) Command: n Partition type: p Partition number: 1 First sector: [Enter] Last sector: +100G Create second data partition (remaining space) Command: n Partition type: p Partition number: 2 First sector: [Enter] Last sector: [Enter] Set partition types Command: t Partition: 1 Hex code: 83 Command: t Partition: 2 Hex code: 83 Write changes Command: w ``` Example 3: Converting MBR to GPT When you need to convert an existing MBR disk to GPT: ```bash First, backup important data! Use gdisk to convert sudo gdisk /dev/sda Command: w # This converts MBR to GPT while preserving partitions ``` Note: This conversion may not always work perfectly, especially with complex partition layouts. Always backup data first. Common Issues and Troubleshooting Issue 1: "Device or resource busy" Error Problem: Cannot partition a mounted device. Solution: ```bash Check what's using the device sudo lsof /dev/sda* sudo fuser -mv /dev/sda* Unmount all partitions sudo umount /dev/sda* If swap is active, disable it sudo swapoff /dev/sda2 ``` Issue 2: Partition Table Not Recognized Problem: System doesn't recognize new partition table. Solution: ```bash Force kernel to re-read partition table sudo partprobe /dev/sda Or use blockdev sudo blockdev --rereadpt /dev/sda Check if partitions are visible lsblk /dev/sda ``` Issue 3: fdisk Shows Wrong Disk Size Problem: fdisk displays incorrect disk capacity. Solution: ```bash Check actual disk size sudo hdparm -I /dev/sda | grep "device size" Update disk geometry sudo hdparm -z /dev/sda Use gdisk for better large disk support sudo gdisk /dev/sda ``` Issue 4: UEFI Boot Issues After Partitioning Problem: System won't boot after creating GPT partitions. Solution: ```bash Ensure EFI System Partition exists and is properly formatted sudo mkfs.fat -F32 /dev/nvme0n1p1 Mount and check EFI partition sudo mkdir /mnt/efi sudo mount /dev/nvme0n1p1 /mnt/efi ls -la /mnt/efi Reinstall bootloader if necessary sudo grub-install --target=x86_64-efi --efi-directory=/mnt/efi ``` Issue 5: Alignment Warnings Problem: fdisk or gdisk shows alignment warnings. Solution: ```bash Check current alignment sudo fdisk -l /dev/sda Use gdisk for better alignment handling sudo gdisk /dev/sda Command: v # Verify alignment For manual alignment, use sector calculations: Start sector should be divisible by 2048 (1MB alignment) ``` Best Practices and Professional Tips Planning Your Partition Layout 1. Assess Your Needs: Determine partition purposes before starting - System partitions (/, /boot, /home) - Data partitions - Swap space requirements - Future expansion needs 2. Size Considerations: ```bash # Root partition: 20-50GB minimum # Swap: 1-2x RAM for systems with <8GB RAM, equal to RAM for >8GB # /home: Remaining space or separate drive # EFI: 512MB (standard) ``` Performance Optimization 1. Proper Alignment: Always align partitions to 1MB boundaries 2. SSD Considerations: Leave 10-20% unpartitioned space for over-provisioning 3. NVMe Optimization: Use GPT for better performance and features Security and Reliability 1. Backup Strategies: ```bash # Backup partition table sudo sfdisk -d /dev/sda > partition-backup.txt # Backup GPT sudo gdisk /dev/sda Command: b # Create binary backup ``` 2. Verification Steps: ```bash # Always verify after partitioning sudo gdisk /dev/sda Command: v # Verify integrity # Check with multiple tools sudo parted /dev/sda print lsblk /dev/sda ``` Automation and Scripting For repetitive partitioning tasks, consider automation: ```bash Automated fdisk example echo -e "n\np\n1\n\n+10G\nw" | sudo fdisk /dev/sdb Automated gdisk example sudo gdisk /dev/sdc << EOF n 1 +512M ef00 w y EOF ``` Documentation and Labeling 1. Partition Labels: Use descriptive names in GPT 2. Documentation: Keep records of partition layouts 3. Filesystem Labels: Label filesystems after creation ```bash sudo e2label /dev/sda1 "Root-System" sudo fatlabel /dev/sda2 "EFI-Boot" ``` Monitoring and Maintenance 1. Regular Checks: ```bash # Check partition health sudo fsck /dev/sda1 # Monitor disk usage df -h # Check for errors sudo dmesg | grep -i error ``` 2. Capacity Planning: Monitor partition usage and plan for expansion Advanced Topics Working with Large Drives (>2TB) For drives larger than 2TB, GPT is mandatory: ```bash Check drive size sudo fdisk -l /dev/sda | head -5 Use gdisk for large drives sudo gdisk /dev/sda Command: p # Shows full capacity ``` RAID Preparation When preparing drives for software RAID: ```bash Set RAID partition type sudo gdisk /dev/sda Command: t Partition: 1 Hex code: fd00 # Linux RAID ``` LVM Integration Preparing partitions for LVM (Logical Volume Management): ```bash Create LVM partition sudo gdisk /dev/sda Command: t Partition: 1 Hex code: 8e00 # Linux LVM ``` Conclusion Mastering disk partitioning with `fdisk` and `gdisk` is essential for effective Linux system administration. This comprehensive guide has covered: - Fundamental concepts of MBR and GPT partitioning schemes - Step-by-step procedures for both fdisk and gdisk operations - Practical examples for common scenarios - Troubleshooting solutions for frequent issues - Best practices for professional implementations Key Takeaways 1. Choose the right tool: Use fdisk for MBR and legacy systems, gdisk for GPT and modern systems 2. Plan carefully: Always design your partition layout before implementation 3. Backup first: Never partition without backing up important data 4. Verify operations: Always check your work before finalizing changes 5. Follow best practices: Proper alignment and sizing improve performance and reliability Next Steps After mastering basic partitioning: 1. Learn filesystem creation and management (`mkfs`, `tune2fs`) 2. Explore advanced storage technologies (LVM, RAID) 3. Study automated deployment and configuration management 4. Practice disaster recovery scenarios 5. Investigate modern storage solutions (ZFS, Btrfs) Remember that partitioning is just the first step in storage management. The skills learned here form the foundation for more advanced storage administration tasks. Always practice these procedures in safe environments before applying them to production systems. With this knowledge, you're well-equipped to handle disk partitioning tasks confidently and efficiently, whether you're managing a single workstation or enterprise storage infrastructure.