How to create partitions with fdisk

How to Create Partitions with fdisk Table of Contents 1. [Introduction](#introduction) 2. [Prerequisites](#prerequisites) 3. [Understanding fdisk Basics](#understanding-fdisk-basics) 4. [Getting Started with fdisk](#getting-started-with-fdisk) 5. [Creating Partitions Step-by-Step](#creating-partitions-step-by-step) 6. [Practical Examples](#practical-examples) 7. [Advanced Partition Management](#advanced-partition-management) 8. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting) 9. [Best Practices and Tips](#best-practices-and-tips) 10. [Conclusion](#conclusion) Introduction The fdisk (fixed disk) command is one of the most fundamental and powerful tools for disk partitioning in Linux and Unix-like systems. Whether you're setting up a new hard drive, preparing storage for a dual-boot system, or managing server storage, understanding how to create partitions with fdisk is an essential skill for system administrators, developers, and power users. This comprehensive guide will walk you through everything you need to know about creating partitions with fdisk, from basic concepts to advanced techniques. You'll learn how to safely partition drives, understand different partition types, handle various scenarios, and troubleshoot common issues that may arise during the partitioning process. By the end of this article, you'll have the confidence and knowledge to effectively use fdisk for all your disk partitioning needs, whether you're working with traditional hard drives, solid-state drives, or other storage devices. Prerequisites Before diving into partition creation with fdisk, ensure you have the following: System Requirements - A Linux or Unix-like operating system - Root or sudo privileges - Access to a terminal or command-line interface - Basic understanding of Linux command-line operations Safety Considerations - Critical: Always backup important data before partitioning - Have a recovery plan in case something goes wrong - Test procedures on non-critical systems first - Understand that partitioning operations can result in data loss Knowledge Prerequisites - Basic understanding of file systems (ext4, NTFS, FAT32) - Familiarity with Linux directory structure - Understanding of primary vs. logical partitions - Basic knowledge of MBR and GPT partition schemes Understanding fdisk Basics What is fdisk? fdisk is a command-line utility that provides a text-based interface for creating, deleting, and modifying disk partitions. It's been a standard tool in Unix-like systems for decades and remains one of the most reliable methods for partition management. Key Features of fdisk - Interactive command-line interface - Support for MBR (Master Boot Record) and GPT (GUID Partition Table) schemes - Ability to create, delete, and modify partitions - Display detailed partition information - Change partition types and flags - Built-in help system Partition Types Overview Primary Partitions - Maximum of 4 primary partitions per disk (MBR) - Can be bootable - Directly addressable by the system Extended Partitions - Special type of primary partition - Acts as a container for logical partitions - Only one extended partition per disk Logical Partitions - Created within extended partitions - No practical limit on number (beyond system constraints) - Cannot be bootable in traditional MBR systems Getting Started with fdisk Accessing fdisk To start using fdisk, you need to identify your target disk and launch the utility with appropriate permissions: ```bash List all available disks sudo fdisk -l Access fdisk for a specific disk (replace /dev/sdX with your target disk) sudo fdisk /dev/sdb ``` Understanding Disk Naming Conventions Linux uses specific naming conventions for storage devices: - SATA/SCSI drives: `/dev/sda`, `/dev/sdb`, `/dev/sdc`, etc. - NVMe drives: `/dev/nvme0n1`, `/dev/nvme0n2`, etc. - IDE drives (legacy): `/dev/hda`, `/dev/hdb`, etc. - Virtual drives: `/dev/vda`, `/dev/vdb`, etc. Initial Disk Assessment Before creating partitions, examine your disk: ```bash Display detailed information about all disks sudo fdisk -l Show partition table for specific disk sudo fdisk -l /dev/sdb Display disk usage df -h ``` Example output: ``` Disk /dev/sdb: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes ``` Creating Partitions Step-by-Step Step 1: Launch fdisk ```bash sudo fdisk /dev/sdb ``` You'll see the fdisk prompt: ``` Welcome to fdisk (util-linux 2.34). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): ``` Step 2: Display Current Partition Table ```bash Command (m for help): p ``` This shows the current partition layout: ``` Disk /dev/sdb: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System ``` Step 3: Create a New Partition ```bash Command (m for help): n ``` fdisk will prompt you for partition details: ``` Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p ``` Choose partition number: ``` Partition number (1-4, default 1): 1 ``` Set the starting sector: ``` First sector (2048-976773167, default 2048): [Press Enter for default] ``` Set the ending sector: ``` Last sector, +sectors or +size{K,M,G,T,P} (2048-976773167, default 976773167): +100G ``` Step 4: Verify the New Partition ```bash Command (m for help): p ``` You should see your new partition listed: ``` Device Boot Start End Blocks Id System /dev/sdb1 2048 209717247 104857600 83 Linux ``` Step 5: Set Partition Type (Optional) ```bash Command (m for help): t Selected partition 1 Partition type (type L to list all types): 83 ``` Common partition types: - 83: Linux native - 82: Linux swap - 7: HPFS/NTFS/exFAT - c: W95 FAT32 (LBA) Step 6: Write Changes to Disk Warning: This step makes changes permanent! ```bash Command (m for help): w ``` Output: ``` The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. ``` Step 7: Format the New Partition ```bash For ext4 filesystem sudo mkfs.ext4 /dev/sdb1 For other filesystems sudo mkfs.ntfs /dev/sdb1 # NTFS sudo mkfs.vfat /dev/sdb1 # FAT32 sudo mkfs.xfs /dev/sdb1 # XFS ``` Practical Examples Example 1: Creating Multiple Partitions on a New Drive Let's partition a 500GB drive with: - 100GB for the operating system - 50GB for swap - Remaining space for data ```bash sudo fdisk /dev/sdb Create first partition (OS) Command (m for help): n Select (default p): p Partition number (1-4, default 1): 1 First sector: [Enter for default] Last sector: +100G Create second partition (swap) Command (m for help): n Select (default p): p Partition number (1-4, default 2): 2 First sector: [Enter for default] Last sector: +50G Create third partition (data) Command (m for help): n Select (default p): p Partition number (1-4, default 3): 3 First sector: [Enter for default] Last sector: [Enter for remaining space] Set swap partition type Command (m for help): t Partition number (1-3, default 3): 2 Partition type: 82 Write changes Command (m for help): w ``` Format the partitions: ```bash sudo mkfs.ext4 /dev/sdb1 sudo mkswap /dev/sdb2 sudo mkfs.ext4 /dev/sdb3 ``` Example 2: Creating an Extended Partition with Logical Partitions When you need more than 4 partitions: ```bash sudo fdisk /dev/sdb Create extended partition Command (m for help): n Select (default p): e Partition number (1-4, default 4): 4 First sector: [Enter for default] Last sector: [Enter for remaining space] Create logical partition within extended Command (m for help): n All primary partitions are in use Adding logical partition 5 First sector: [Enter for default] Last sector: +50G Create another logical partition Command (m for help): n All primary partitions are in use Adding logical partition 6 First sector: [Enter for default] Last sector: +100G Write changes Command (m for help): w ``` Example 3: Preparing a Drive for Dual Boot Setting up partitions for Windows and Linux dual boot: ```bash sudo fdisk /dev/sda Windows partition (NTFS) Command (m for help): n Select (default p): p Partition number: 1 First sector: [Enter] Last sector: +200G Command (m for help): t Partition type: 7 Linux root partition Command (m for help): n Select (default p): p Partition number: 2 First sector: [Enter] Last sector: +80G Linux swap Command (m for help): n Select (default p): p Partition number: 3 First sector: [Enter] Last sector: +8G Command (m for help): t Partition number: 3 Partition type: 82 Linux home partition Command (m for help): n Select (default p): p Partition number: 4 First sector: [Enter] Last sector: [Enter] Write changes Command (m for help): w ``` Advanced Partition Management Working with GPT Partition Tables For drives larger than 2TB or when you need more than 4 primary partitions, use GPT: ```bash Create GPT partition table sudo fdisk /dev/sdb Command (m for help): g Create partitions normally Command (m for help): n Partition number (1-128, default 1): 1 First sector: [Enter] Last sector: +100G ``` Using fdisk with Scripts For automated partitioning: ```bash #!/bin/bash Automated partitioning script DISK="/dev/sdb" Create partition table and partitions sudo fdisk $DISK << EOF o n p 1 +100G n p 2 +50G t 2 82 w EOF Format partitions sudo mkfs.ext4 ${DISK}1 sudo mkswap ${DISK}2 ``` Partition Alignment and Performance Modern drives benefit from proper alignment: ```bash Check current alignment sudo fdisk -l /dev/sdb | grep "Sector size" For SSDs, align to 1MB boundaries fdisk automatically handles this for most modern drives ``` Common Issues and Troubleshooting Issue 1: "Device or resource busy" Error Problem: Cannot access disk because it's in use. Solution: ```bash Check what's using the disk sudo lsof /dev/sdb* sudo fuser -v /dev/sdb* Unmount mounted partitions sudo umount /dev/sdb1 Stop any processes using the disk sudo fuser -k /dev/sdb* ``` Issue 2: Partition Table Corruption Problem: fdisk reports errors reading partition table. Solution: ```bash Backup current partition table sudo dd if=/dev/sdb of=partition-backup.img bs=512 count=1 Try to fix with fsck sudo fsck /dev/sdb1 Recreate partition table if necessary sudo fdisk /dev/sdb Command (m for help): o # Create new empty DOS partition table ``` Issue 3: Cannot Create More Than 4 Partitions Problem: MBR limitation of 4 primary partitions. Solution: ```bash Option 1: Convert to GPT sudo fdisk /dev/sdb Command (m for help): g Option 2: Use extended partition Command (m for help): n Select (default p): e # Create extended partition ``` Issue 4: Partition Not Recognized After Creation Problem: System doesn't see new partition. Solution: ```bash Force kernel to re-read partition table sudo partprobe /dev/sdb Or restart udev sudo udevadm settle Check if partition appears ls -la /dev/sdb* ``` Issue 5: fdisk Shows Wrong Disk Size Problem: fdisk reports incorrect disk capacity. Solution: ```bash Check physical disk size sudo hdparm -I /dev/sdb | grep "device size" Update disk geometry sudo fdisk /dev/sdb Command (m for help): x # Expert mode Expert command (m for help): r # Return to main menu ``` Issue 6: Accidental Partition Deletion Problem: Accidentally deleted important partition. Solution: ```bash DO NOT write changes to disk! Use 'q' to quit without saving Use testdisk for recovery sudo apt-get install testdisk sudo testdisk /dev/sdb Or photorec for file recovery sudo photorec /dev/sdb ``` Best Practices and Tips Planning Your Partition Scheme 1. Assess your needs: Determine required partition sizes based on intended use 2. Leave room for growth: Don't use 100% of available space 3. Consider backup strategies: Plan for backup partition or external storage 4. Think about performance: Separate frequently accessed data Safety Recommendations 1. Always backup data before partitioning 2. Test on virtual machines first 3. Use 'p' command frequently to check partition table 4. Don't write changes until you're certain 5. Keep rescue media available Performance Optimization ```bash Check partition alignment sudo fdisk -l /dev/sdb | grep -E "(Start|Sector)" For SSDs, ensure TRIM support sudo fstrim -v /mount/point Set appropriate filesystem options sudo tune2fs -o discard /dev/sdb1 ``` Partition Sizing Guidelines | Partition Type | Recommended Size | Notes | |----------------|------------------|-------| | Root (/) | 20-50GB | Minimum for OS | | Home (/home) | 50GB+ | User data storage | | Swap | 1-2x RAM | For hibernation support | | Boot (/boot) | 500MB-1GB | Kernel and bootloader | | Var (/var) | 10-20GB | Logs and temporary files | Automation and Scripting Create reusable partition scripts: ```bash #!/bin/bash partition-setup.sh DISK=$1 if [ -z "$DISK" ]; then echo "Usage: $0 /dev/sdX" exit 1 fi echo "Partitioning $DISK..." Verify disk exists if [ ! -b "$DISK" ]; then echo "Error: $DISK is not a valid block device" exit 1 fi Create partitions sudo fdisk $DISK << EOF o n p 1 +20G n p 2 +4G t 2 82 n p 3 w EOF echo "Partitioning complete!" ``` Monitoring and Maintenance ```bash Regular partition health checks sudo fsck -n /dev/sdb1 # Check without fixing Monitor disk usage df -h du -sh /* Check for bad sectors sudo badblocks -v /dev/sdb1 ``` Conclusion Creating partitions with fdisk is a fundamental skill that every Linux administrator and power user should master. Throughout this comprehensive guide, we've covered everything from basic concepts to advanced techniques, providing you with the knowledge and confidence to handle various partitioning scenarios. Key takeaways from this guide include: - Understanding the basics: fdisk is a powerful, reliable tool for disk partitioning that has stood the test of time - Safety first: Always backup your data and plan your partitioning strategy before making changes - Practical application: We've covered real-world scenarios including multi-partition setups, dual-boot configurations, and server storage management - Troubleshooting skills: Common issues have solutions, and knowing how to diagnose and fix problems is crucial - Best practices: Following established guidelines helps ensure successful partitioning operations and optimal system performance Next Steps Now that you've mastered fdisk partition creation, consider exploring these related topics: 1. Advanced filesystem management with tools like parted and gparted 2. LVM (Logical Volume Management) for more flexible storage solutions 3. RAID configuration for redundancy and performance 4. Automated deployment scripts for consistent system provisioning 5. Storage monitoring and maintenance best practices Final Recommendations - Practice these techniques in a safe environment before applying them to production systems - Keep this guide bookmarked for reference during actual partitioning operations - Stay updated with the latest fdisk features and Linux storage technologies - Consider learning complementary tools like parted for GUI-based partition management Remember that disk partitioning is a critical system operation that requires careful planning and execution. With the knowledge gained from this guide, you're well-equipped to handle your partitioning needs safely and effectively. Whether you're setting up a new server, preparing a workstation, or managing storage for development environments, fdisk remains an indispensable tool in your Linux toolkit.