Master Linux Disk Partitioning: From Primary to Swap in Minutes
This guide explains Linux disk partitioning fundamentals, covering primary and logical partitions, MBR structure, fdisk commands, creating and formatting partitions, mounting filesystems, handling large disks, repairing bad drives, and setting up swap space, with step‑by‑step code examples for system administrators.
Primary and Logical Partitions
Linux Partitioning Basics
Disk partitioning formats the disk so it can store data; creating a partition also sets physical parameters, the Master Boot Record (MBR), and its backup location.
MBR overview: the first sector contains boot loader code (446 bytes), a partition table of four entries (16 bytes each), and a 2‑byte boot signature (0xAA55).
Partition numbers: primary partitions 1‑4, logical partitions start at 5.
Linux rule: logical partitions must be created inside an extended partition, not directly on a primary partition.
Partition purposes:
Primary partition – typically holds the OS bootloader; /boot is best placed here.
Extended partition – cannot be used directly; it serves as a container for logical partitions.
Data is stored in both primary and logical partitions, with large amounts usually placed in logical partitions.
Use the fdisk tool for partitioning, formatting, and other operations.
Note: A maximum of four primary/extended partitions is allowed. Only one extended partition can exist. Logical partitions can be zero or many.
Disk Management Commands
fdisk : Linux partition table utility
# Common operations
fdisk -l # List current partitions
# Menu commands
n : add new partition
p : view partition info
w : write and exit
q : quit without saving
d : delete partition
t : change partition typeViewing Disk Information
ls /dev/sd* # List disk devicesDevice letters a‑z represent disk identifiers (e.g., sda is the first SCSI disk, sdb the second, etc.).
Create Primary Partition Demo
ls /dev/sd*
fdisk /dev/sdb
# ... interactive fdisk session ...
# Example: create a 5 GiB primary partition on /dev/sdb
# Save and exit with 'w'Check Partition
ls /dev/sd*Format to XFS
mkfs.xfs /dev/sdb1Mount to Local Directory
mkdir /xfs_du
mount /dev/sdb1 /xfs_duView Mount Information
df -hEnable Automatic Mount at Boot
echo "/dev/sdb1 /xfs_du xfs defaults 0 0" >> /etc/fstab
# or
echo "mount /dev/sdb1 /xfs_du" >> /etc/rc.local
chmod +x /etc/rc.d/rc.localLogical Partition Creation with fdisk
Add Extended Partition
fdisk /dev/sdb
# Choose 'e' for extended, set size (e.g., +5G)Create Logical Partition
fdisk /dev/sdb
# Choose 'l' for logical, set size (e.g., +2G)Create Filesystem on Logical Partition
mkfs.xfs /dev/sdb5Automatic Mount for Logical Partition
echo "mount /dev/sdb5 /xfs_du" >> /etc/rc.localBad Disk Repair (Partitions on >8 TB Disks)
Identify Faulty Disk
sudo lsscsiRun a write test (e.g., dd if=/dev/zero of=wtest_find_error_disk bs=64k count=100k) on each disk; the disk that does not cause errors is healthy.
Initialize New Disk, Create Partition, Format Filesystem
View Current Partitions
fdisk -l # Note: use GPT for disks >2 TBCheck Existing Partition Table Type
parted /dev/sdX printDefine Partition Table
# GPT
sudo parted -s /dev/sdX mklabel gpt
# MBR
sudo parted -s /dev/sdX mklabel msdosCreate Partition
sudo parted -s /dev/sdX mkpart primary 0% 100%Verify Partition
fdisk -lFormat New Partition
sudo mkfs.xet4 /dev/sdX1Swap Partition
Swap provides virtual memory by using disk space when physical RAM is insufficient.
Create Swap in a Local Directory
Create Directory
mkdir /swapCreate Empty File
dd if=/dev/zero of=/swap/swap bs=2M count=2014Format as Swap
mkswap /swap/swapAdd to /etc/fstab for Automatic Mount
echo "/swap/swap swap swap defaults 0 0" >> /etc/fstabSet Permissions and Mount
chmod 0600 /swap/swap
mount -aEnable Swap
swapon -aDisable Swap
swapoff -aSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
