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.
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 type1.3 View disks
ls /dev/sd* # list disk devicesDevice 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
w1.4.2 Verify partition
fdisk -l /dev/sdb1.4.3 Format to XFS
mkfs.xfs /dev/sdb11.4.4 Mount locally
mkdir /xfs_du
mount /dev/sdb1 /xfs_du1.4.5 Check mount
df -h1.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.local1.5 fdisk demo – create logical partition
1.5.1 Add extended partition
fdisk /dev/sdb
n
e
+5G
w1.5.2 Create logical partition
fdisk /dev/sdb
n
l
+2G
w1.5.3 Create filesystem on logical partition
mkfs.xfs /dev/sdb51.5.4 Auto‑mount logical partition
echo "mount /dev/sdb5 /xfs_du" >> /etc/rc.local2 Bad‑disk recovery (disks >8 TB)
2.1 Identify faulty disk
2.1.1 List SCSI devices
sudo lsscsi2.1.2 IO test to locate bad disk
dd oflag=direct if=/dev/zero of=wtest_find_error_disk bs=64k count=100k2.2 Initialize new disk, partition, format
2.2.1 List current partitions
fdisk -l # note: use GPT for >2 TB disks2.2.2 Check partition table type
parted /dev/sdX print list2.2.3 Create partition table
sudo parted -s /dev/sdX mklabel gpt # or msdos2.2.4 Create partition
sudo parted -s /dev/sdX mkpart primary 0% 100%2.2.5 Verify
fdisk -l2.2.6 Format filesystem
sudo mkfs.xet4 /dev/sdX13 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 /swap3.1.2 Create empty file
dd if=/dev/zero of=/swap/swap bs=2M count=20143.1.3 Format as swap
mkswap /swap/swap3.1.4 Add to /etc/fstab for auto‑mount
echo "/swap/swap swap swap defaults 0 0" >> /etc/fstab3.1.5 Set permissions and mount
chmod 0600 /swap/swap
mount -a3.1.6 Enable swap
swapon -a3.1.7 Disable 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.
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.
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.
