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
fdisktool 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
<code>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 type
</code>Viewing Disk Information
<code>ls /dev/sd* # List disk devices</code>Device letters a‑z represent disk identifiers (e.g., sda is the first SCSI disk, sdb the second, etc.).
Create Primary Partition Demo
<code>ls /dev/sd*
fdisk /dev/sdb
# ... interactive fdisk session ...
# Example: create a 5 GiB primary partition on /dev/sdb
# Save and exit with 'w'
</code>Check Partition
<code>ls /dev/sd*</code>Format to XFS
<code>mkfs.xfs /dev/sdb1</code>Mount to Local Directory
<code>mkdir /xfs_du
mount /dev/sdb1 /xfs_du</code>View Mount Information
<code>df -h</code>Enable Automatic Mount at Boot
<code>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.local</code>Logical Partition Creation with fdisk
Add Extended Partition
<code>fdisk /dev/sdb
# Choose 'e' for extended, set size (e.g., +5G)
</code>Create Logical Partition
<code>fdisk /dev/sdb
# Choose 'l' for logical, set size (e.g., +2G)
</code>Create Filesystem on Logical Partition
<code>mkfs.xfs /dev/sdb5</code>Automatic Mount for Logical Partition
<code>echo "mount /dev/sdb5 /xfs_du" >> /etc/rc.local</code>Bad Disk Repair (Partitions on >8 TB Disks)
Identify Faulty Disk
<code>sudo lsscsi</code>Run 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
<code>fdisk -l # Note: use GPT for disks >2 TB</code>Check Existing Partition Table Type
<code>parted /dev/sdX print</code>Define Partition Table
<code># GPT
sudo parted -s /dev/sdX mklabel gpt
# MBR
sudo parted -s /dev/sdX mklabel msdos
</code>Create Partition
<code>sudo parted -s /dev/sdX mkpart primary 0% 100%</code>Verify Partition
<code>fdisk -l</code>Format New Partition
<code>sudo mkfs.xet4 /dev/sdX1</code>Swap Partition
Swap provides virtual memory by using disk space when physical RAM is insufficient.
Create Swap in a Local Directory
Create Directory
<code>mkdir /swap</code>Create Empty File
<code>dd if=/dev/zero of=/swap/swap bs=2M count=2014</code>Format as Swap
<code>mkswap /swap/swap</code>Add to /etc/fstab for Automatic Mount
<code>echo "/swap/swap swap swap defaults 0 0" >> /etc/fstab</code>Set Permissions and Mount
<code>chmod 0600 /swap/swap
mount -a</code>Enable Swap
<code>swapon -a</code>Disable Swap
<code>swapoff -a</code>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.