Chapter Objective: Partition a disk, create and mount filesystems, configure persistent mounts through /etc/fstab, and create and enable swap space.
Key commands: parted, mkfs.xfs, blkid, mount -a, mkswap, swapon
Storage Concepts
| Term | Meaning |
|---|---|
| Disk / block device | The raw physical (or virtual) storage device, e.g. /dev/sdb |
| Partition | A defined section of a disk, e.g. /dev/sdb1 |
| Filesystem | The structure that organizes files and directories on a partition |
| Mount point | The directory a filesystem is attached to and accessed through |
🔵 Why It Matters
These four layers — disk, partition, filesystem, mount point — are distinct steps, and storage problems usually trace back to exactly one of them. Knowing which layer you're troubleshooting narrows the search dramatically.
Partitioning with parted
# Confirm the device you're about to partition (always double-check!)
lsblk
# Open parted interactively on a specific disk
sudo parted /dev/sdb
# Inside parted: create a GPT partition table (required before adding partitions)
(parted) mklabel gpt
# Create a primary partition spanning most of the disk
(parted) mkpart primary 0% 100%
# Show the current partition layout
(parted) print
# Exit parted
(parted) quit
# Or, non-interactively in a single command line
sudo parted /dev/sdb --script mklabel gpt mkpart primary 0% 100%
# Confirm the kernel sees the new partition
lsblk /dev/sdb
⚠️ Warning — parted Applies Changes Immediately
Unlike some older partitioning tools,
parted writes each change to disk as soon as you issue the command — there's no separate "save" step to reconsider before it's applied. Confirm the target device with lsblk before you start.
Creating Filesystems
# Create an XFS filesystem (RHEL's default) on the new partition
sudo mkfs.xfs /dev/sdb1
# Or ext4, if that's what you need
sudo mkfs.ext4 /dev/sdb1
# Confirm the filesystem type and get its UUID
blkid /dev/sdb1
| Filesystem | Notes |
|---|---|
| XFS | RHEL's default; scales well to very large filesystems, but cannot be shrunk |
| ext4 | Mature and widely compatible; supports both growing and shrinking |
✅ Tip — Know XFS's Shrink Limitation
If there's any realistic chance you'll need to shrink a filesystem later, that's a point in favor of ext4 over XFS — XFS filesystems can be grown, but not reduced in size, without recreating them.
Mounting Persistently with /etc/fstab
A manual mount command (Chapter 14 of RH124) doesn't survive a reboot. For a mount that should always be there, add an entry to /etc/fstab.
# /etc/fstab fields:
# device mount-point fs-type options dump pass
UUID=1a2b3c4d-... /data xfs defaults 0 2
# Get the UUID to use in fstab (preferred over a raw device name,
# which can shift if disks are added/removed)
blkid /dev/sdb1
# Create the mount point
sudo mkdir -p /data
# After editing /etc/fstab, test it WITHOUT rebooting
sudo mount -a
# Confirm it mounted as expected
df -h /data
⚠️ Warning — A Bad fstab Entry Can Prevent Booting
A syntax error or a device that fails to mount at boot can drop the system into an emergency shell during startup. Always run
mount -a to test a new entry before rebooting — it will surface most errors immediately, safely.
✅ Tip — Prefer UUID Over /dev/sdX
Device names like
/dev/sdb1 can change across reboots depending on device enumeration order, especially with multiple disks. A UUID is stable and tied to the filesystem itself, not its detected position.
Creating and Enabling Swap Space
# Format a partition as swap
sudo mkswap /dev/sdb2
# Enable it immediately
sudo swapon /dev/sdb2
# Confirm it's active
swapon --show
free -h
# Make it persistent by adding to /etc/fstab
UUID=5e6f7a8b-... none swap defaults 0 0
🔵 Note — A Swap File Works Too
Swap doesn't have to be a dedicated partition — a regular file, sized with
fallocate or dd and then formatted with mkswap, works identically and is often more flexible to resize later.
Checking Filesystem Health and Space
# Disk space usage across mounted filesystems
df -h
# Check and repair a filesystem (UNMOUNTED only)
sudo umount /data
sudo fsck /dev/sdb1
sudo mount /data
⚠️ Warning — Never Run fsck on a Mounted Filesystem
Running
fsck against an actively mounted filesystem risks corrupting it further, since the running system may be writing to it at the same time the check is trying to repair it. Always unmount first.
Key Terms for Chapter 10
- partition table
- The structure on a disk defining how it's divided into partitions (e.g. GPT)
- parted
- Utility for creating and modifying disk partitions, applying changes immediately
- mkfs
- Family of commands that create a filesystem on a partition (e.g.
mkfs.xfs) - /etc/fstab
- Configuration file defining filesystems to mount automatically at boot
- UUID
- A unique, stable identifier for a filesystem, preferred over device names in fstab
- swap
- Disk space used to extend available memory when RAM is under pressure
- fsck
- Utility that checks and repairs filesystem inconsistencies; must be run on an unmounted filesystem
Review Questions
- What are the four distinct layers between "a physical disk" and "a file you can open," in order?
- Why should you always confirm a device name with
lsblkbefore runningpartedagainst it? - What's a key practical limitation of XFS compared to ext4?
- Why is it recommended to use a UUID rather than a device name like
/dev/sdb1in/etc/fstab? - After editing
/etc/fstab, what command lets you test the new entry without rebooting? - Write the two commands needed to format
/dev/sdc1as swap and activate it immediately. - Why must a filesystem be unmounted before running
fsckon it?