Operations 14 min read

Master Linux Disk Partitioning: From MBR Basics to Automated XFS Setup

This guide walks through Linux primary and logical partition concepts, MBR structure, fdisk commands, creating and formatting partitions with XFS, automating mounts, handling large or faulty disks, and setting up swap space for virtual memory.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Disk Partitioning: From MBR Basics to Automated XFS Setup

1 Primary and Logical Partitions

1.1 Linux partitioning basics

Disk partitioning formats the disk and sets up the Master Boot Record (MBR) and its backup.

MBR consists of a 446‑byte boot loader, a partition table with four 16‑byte entries, and a 2‑byte boot signature (0xAA55).

Partition numbers: primary 1‑4, logical start at 5.

Linux requires logical partitions to reside within an extended partition.

Primary partitions are used for booting (/boot); extended partitions are containers; data is stored in primary or logical partitions.

Use fdisk for partitioning and formatting.

Maximum of four primary+extended partitions; only one extended partition; logical partitions can be multiple.

1.2 Disk management commands

fdisk – Linux partition table tool

# Common operations
fdisk -l   # list partitions
n          # add new partition
p          # primary
e          # extended
w          # write and exit
q          # quit without saving
d          # delete
t          # change type

1.3 View disks

ls /dev/sd*   # list disk devices
Device letters a‑z represent disk numbers (sda, sdb, …). n indicates partition number.

1.4 Creating a primary partition (demo)

1.4.1 Add disk and create primary partition

ls /dev/sd*
fdisk /dev/sdb
# create primary partition of 5 GiB
n
p
1
<enter>
+5G
w

1.4.2 Verify partition

fdisk -l /dev/sdb

1.4.3 Format to XFS

mkfs.xfs /dev/sdb1

1.4.4 Mount locally

mkdir /xfs_du
mount /dev/sdb1 /xfs_du

1.4.5 Check mount

df -h

1.4.6 Enable auto‑mount at boot

echo "/dev/sdb1 /xfs_du xfs defaults 0 0" >> /etc/fstab
# alternative method
echo "mount /dev/sdb1 /xfs_du" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local

1.5 fdisk demo – create logical partition

1.5.1 Add extended partition

fdisk /dev/sdb
n
e
+5G
w

1.5.2 Create logical partition

fdisk /dev/sdb
n
l
+2G
w

1.5.3 Create filesystem on logical partition

mkfs.xfs /dev/sdb5

1.5.4 Auto‑mount logical partition

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

2 Bad‑disk recovery (disks >8 TB)

2.1 Identify faulty disk

2.1.1 List SCSI devices

sudo lsscsi

2.1.2 IO test to locate bad disk

dd oflag=direct if=/dev/zero of=wtest_find_error_disk bs=64k count=100k

2.2 Initialize new disk, partition, format

2.2.1 List current partitions

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

2.2.2 Check partition table type

parted /dev/sdX print list

2.2.3 Create partition table

sudo parted -s /dev/sdX mklabel gpt   # or msdos

2.2.4 Create partition

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

2.2.5 Verify

fdisk -l

2.2.6 Format filesystem

sudo mkfs.xet4 /dev/sdX1

3 Swap partition

Swap provides virtual memory when physical RAM is insufficient.

3.1 Create swap in a local directory

3.1.1 Create directory

mkdir /swap

3.1.2 Create empty file

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

3.1.3 Format as swap

mkswap /swap/swap

3.1.4 Add to /etc/fstab for auto‑mount

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

3.1.5 Set permissions and mount

chmod 0600 /swap/swap
mount -a

3.1.6 Enable swap

swapon -a

3.1.7 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.

LinuxSystem AdministrationSwapdisk partitioningfdiskXFS
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.