Operations 14 min read

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.

Raymond Ops
Raymond Ops
Raymond Ops
Master Linux Disk Partitioning: From Primary to Swap in Minutes

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 type

Viewing Disk Information

ls /dev/sd*    # List disk devices
Device 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/sdb1

Mount to Local Directory

mkdir /xfs_du
mount /dev/sdb1 /xfs_du

View Mount Information

df -h

Enable 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.local

Logical 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/sdb5

Automatic Mount for Logical Partition

echo "mount /dev/sdb5 /xfs_du" >> /etc/rc.local

Bad Disk Repair (Partitions on >8 TB Disks)

Identify Faulty Disk

sudo lsscsi

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

fdisk -l   # Note: use GPT for disks >2 TB

Check Existing Partition Table Type

parted /dev/sdX print

Define Partition Table

# GPT
sudo parted -s /dev/sdX mklabel gpt
# MBR
sudo parted -s /dev/sdX mklabel msdos

Create Partition

sudo parted -s /dev/sdX mkpart primary 0% 100%

Verify Partition

fdisk -l

Format New Partition

sudo mkfs.xet4 /dev/sdX1

Swap Partition

Swap provides virtual memory by using disk space when physical RAM is insufficient.

Create Swap in a Local Directory

Create Directory

mkdir /swap

Create Empty File

dd if=/dev/zero of=/swap/swap bs=2M count=2014

Format as Swap

mkswap /swap/swap

Add to /etc/fstab for Automatic Mount

echo "/swap/swap swap swap defaults 0 0" >> /etc/fstab

Set Permissions and Mount

chmod 0600 /swap/swap
mount -a

Enable Swap

swapon -a

Disable Swap

swapoff -a
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxdisk partitioningfdisk
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.