Chapter 9 ⏱️ 55 min read πŸ“š Intermediate

Manage Basic Storage

Master disk partitioning, file system creation, and storage mounting. Learn MBR/GPT partitioning schemes, create ext4 and XFS filesystems, configure swap space, and make mounts persistent with /etc/fstab.

🎯 Introduction

Storage management is fundamental to Linux administration. You must understand how to partition disks, create file systems, and mount storage to make it accessible. RHEL supports both MBR (legacy) and GPT (modern) partitioning schemes.

Storage Hierarchy

  1. Physical Disk: /dev/sda, /dev/vda, /dev/nvme0n1
  2. Partitions: /dev/sda1, /dev/vda2, /dev/nvme0n1p1
  3. File System: ext4, XFS, Btrfs
  4. Mount Point: /, /home, /data

Device Naming Conventions

Device Type Naming Pattern Example
SATA/SAS Disks /dev/sd[a-z] /dev/sda, /dev/sdb
Virtual Disks (KVM) /dev/vd[a-z] /dev/vda, /dev/vdb
NVMe SSDs /dev/nvme[0-9]n[0-9] /dev/nvme0n1, /dev/nvme1n1
MBR Partitions /dev/sda[1-4] /dev/sda1, /dev/sda2
GPT/NVMe Partitions /dev/nvme0n1p[1-128] /dev/nvme0n1p1
πŸ“˜ MBR vs GPT

MBR: Legacy, max 2TB disks, 4 primary partitions
GPT: Modern, supports >2TB disks, up to 128 partitions, more reliable

πŸ”§ Disk Partitioning

Viewing Disk Information

# List all block devices
lsblk

# Output example:
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   20G  0 disk 
β”œβ”€sda1   8:1    0    1G  0 part /boot
└─sda2   8:2    0   19G  0 part 
  β”œβ”€rhel-root 253:0 0 17G 0 lvm  /
  └─rhel-swap 253:1 0  2G 0 lvm  [SWAP]
sdb      8:16   0   10G  0 disk 

# Show filesystem information
lsblk -f

# List partition tables
sudo fdisk -l

# Show disk usage
df -h

# Show partition UUID and filesystem
sudo blkid

Partitioning with fdisk (MBR)

Use fdisk for MBR partition tables (disks ≀ 2TB):

# Start fdisk
sudo fdisk /dev/sdb

# Commands within fdisk:
m     # Help
p     # Print partition table
n     # New partition
d     # Delete partition
t     # Change partition type
l     # List partition types
w     # Write changes and exit
q     # Quit without saving

# Example: Create new partition
Command: n
Partition type: p (primary)
Partition number: 1
First sector: (press Enter for default)
Last sector: +2G (create 2GB partition)

# Set partition type to Linux LVM (type 8e)
Command: t
Partition number: 1
Hex code: 8e

# Write changes
Command: w

Partitioning with parted (GPT)

Use parted for GPT partition tables (modern, >2TB disks):

# Start parted in interactive mode
sudo parted /dev/sdb

# Create GPT partition table
(parted) mklabel gpt

# Create partition
(parted) mkpart primary 1MiB 2GiB

# Name the partition
(parted) name 1 data

# Set flags
(parted) set 1 lvm on

# Print partition table
(parted) print

# Quit
(parted) quit

# Non-interactive mode
sudo parted /dev/sdb mklabel gpt
sudo parted /dev/sdb mkpart primary 1MiB 2GiB
sudo parted /dev/sdb name 1 data
sudo parted /dev/sdb set 1 lvm on

Updating Kernel Partition Table

# Inform kernel of partition table changes
sudo partprobe /dev/sdb

# Alternative if partprobe fails
sudo partx -u /dev/sdb

# Verify changes
lsblk /dev/sdb

Procedure: Adding a New 5GB Data Partition

  1. Identify available disk:
    lsblk
    # Assume /dev/sdb is available
  2. Create GPT partition table:
    sudo parted /dev/sdb mklabel gpt
  3. Create 5GB partition:
    sudo parted /dev/sdb mkpart primary 1MiB 5GiB
  4. Update kernel:
    sudo partprobe /dev/sdb
  5. Verify partition:
    lsblk /dev/sdb
    sudo parted /dev/sdb print

Deleting Partitions

# With parted
sudo parted /dev/sdb rm 1

# With fdisk
sudo fdisk /dev/sdb
Command: d
Partition number: 1
Command: w

πŸ’Ύ File Systems

RHEL 9 primarily uses XFS (default) and ext4 file systems.

File System Comparison

Feature XFS ext4
Max file size 8 EiB 16 TiB
Max filesystem 8 EiB 1 EiB
Can shrink No Yes
Default RHEL 9 Yes No

Creating File Systems

# Create XFS filesystem (RHEL default)
sudo mkfs.xfs /dev/sdb1

# Create ext4 filesystem
sudo mkfs.ext4 /dev/sdb1

# Create with label
sudo mkfs.xfs -L "data" /dev/sdb1
sudo mkfs.ext4 -L "backup" /dev/sdb2

# Force creation (overwrites existing filesystem)
sudo mkfs.xfs -f /dev/sdb1

# Create with specific options
sudo mkfs.ext4 -b 4096 -i 8192 /dev/sdb1
# -b block size
# -i bytes per inode

File System Labels

# Set/change XFS label
sudo xfs_admin -L "newlabel" /dev/sdb1

# Set/change ext4 label
sudo e2label /dev/sdb1 "newlabel"

# View labels
lsblk -f
sudo blkid

Checking and Repairing File Systems

# Check XFS filesystem
sudo xfs_repair /dev/sdb1

# Check ext4 filesystem
sudo fsck.ext4 /dev/sdb1
# or
sudo e2fsck /dev/sdb1

# Force check
sudo e2fsck -f /dev/sdb1

# Automatic repair
sudo e2fsck -p /dev/sdb1

# Interactive repair
sudo e2fsck -y /dev/sdb1
⚠️ File System Checks

Never run fsck/xfs_repair on a mounted filesystem! Always unmount first or the system will be damaged. Use umount /dev/sdb1 before checking.

Extending File Systems

# Extend XFS (must be mounted)
sudo xfs_growfs /mount/point

# Extend ext4 (can be mounted or unmounted)
sudo resize2fs /dev/sdb1

# Extend to specific size
sudo resize2fs /dev/sdb1 10G

πŸ“‚ Mounting File Systems

Manual Mounting

# Create mount point
sudo mkdir /data

# Mount filesystem
sudo mount /dev/sdb1 /data

# Mount with options
sudo mount -o ro /dev/sdb1 /data        # Read-only
sudo mount -o noexec /dev/sdb1 /data    # No execute
sudo mount -o nosuid /dev/sdb1 /data    # Ignore SUID

# Mount by label
sudo mount LABEL=data /data

# Mount by UUID
sudo mount UUID=xxx-xxx-xxx /data

# Verify mount
mount | grep sdb1
df -h /data

Unmounting

# Unmount by device
sudo umount /dev/sdb1

# Unmount by mount point
sudo umount /data

# Force unmount (if busy)
sudo umount -f /data

# Lazy unmount (detach now, cleanup when not busy)
sudo umount -l /data

# Check what's using the mount
sudo lsof /data
sudo fuser -mv /data

Common Mount Options

Option Description
defaults rw, suid, dev, exec, auto, nouser, async
ro Read-only
rw Read-write
noexec Do not allow execution of binaries
nosuid Ignore SUID and SGID bits
noauto Do not mount automatically at boot
_netdev Network device (wait for network)

πŸ’± Swap Space

Swap is used when RAM is full. It can be a partition or a file.

Creating Swap Partition

# Create partition (use partition type 82 for MBR, or 'swap' for GPT)
sudo parted /dev/sdb mkpart primary linux-swap 5GiB 7GiB

# Format as swap
sudo mkswap /dev/sdb2

# Set label
sudo mkswap -L swap1 /dev/sdb2

# Enable swap
sudo swapon /dev/sdb2

# Verify
swapon --show
free -h

Creating Swap File

# Create 2GB swap file
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
# or faster with fallocate
sudo fallocate -l 2G /swapfile

# Set permissions
sudo chmod 600 /swapfile

# Format as swap
sudo mkswap /swapfile

# Enable swap
sudo swapon /swapfile

# Verify
swapon --show

Managing Swap

# View swap usage
swapon --show
free -h
cat /proc/swaps

# Disable swap
sudo swapoff /dev/sdb2
sudo swapoff /swapfile

# Disable all swap
sudo swapoff -a

# Enable all swap from /etc/fstab
sudo swapon -a

# Check swap priority
swapon --show

Swap Priority

# Enable with priority (higher number = higher priority)
sudo swapon -p 10 /dev/sdb2

# In /etc/fstab:
/dev/sdb2  none  swap  pri=10  0 0
πŸ’‘ Swap Size Recommendations

RAM ≀ 2GB: 2x RAM
RAM 2-8GB: Same as RAM
RAM >8GB: At least 4GB, or 0.5x RAM
For hibernation: RAM size + 0.5GB

πŸ“ Persistent Mounts (/etc/fstab)

The /etc/fstab file defines filesystems to mount at boot time.

fstab Format

# /etc/fstab
#           

UUID=xxx-xxx   /data         xfs     defaults   0       2
LABEL=backup   /backup       ext4    defaults   0       2
/dev/sdb3      /mnt/extra    xfs     noauto     0       0
/swapfile      none          swap    sw         0       0

Field Descriptions

Field Description
Device UUID, LABEL, or /dev/sdX (UUID preferred)
Mount Point Where to mount (use 'none' for swap)
Type xfs, ext4, swap, nfs, etc.
Options defaults, ro, noexec, etc.
Dump 0=no backup, 1=backup (usually 0)
Pass 0=no fsck, 1=root fs, 2=other fs

Getting UUID

# Show all UUIDs
sudo blkid

# Show specific device
sudo blkid /dev/sdb1

# Output:
/dev/sdb1: UUID="xxx-xxx-xxx" TYPE="xfs"

# Use lsblk
lsblk -f

Procedure: Adding Persistent Mount

  1. Get the UUID:
    sudo blkid /dev/sdb1
    UUID="a1b2c3d4-e5f6-..."
  2. Create mount point:
    sudo mkdir /data
  3. Edit /etc/fstab:
    sudo vi /etc/fstab
    
    # Add line:
    UUID=a1b2c3d4-e5f6-...  /data  xfs  defaults  0  2
  4. Test the fstab entry (without rebooting):
    sudo mount -a
  5. Verify:
    df -h /data
    mount | grep /data
  6. Test fstab syntax:
    sudo findmnt --verify
⚠️ Critical: Test Before Reboot

Always test /etc/fstab entries with mount -a before rebooting! An incorrect fstab can prevent the system from booting. If that happens, boot into rescue mode and fix the fstab file.

Systemd Mount Units

Alternative to fstab for complex mounts:

# Create mount unit: /etc/systemd/system/data.mount
[Unit]
Description=Data Partition

[Mount]
What=/dev/sdb1
Where=/data
Type=xfs
Options=defaults

[Install]
WantedBy=multi-user.target

# Enable and start
sudo systemctl daemon-reload
sudo systemctl enable --now data.mount

πŸ“ Practice Questions

Question 1: Which partition scheme supports disks larger than 2TB?

  • A) MBR
  • B) GPT
  • C) LVM
  • D) Extended partition
Answer: B) GPT
GPT (GUID Partition Table) supports disks >2TB and up to 128 partitions. MBR is limited to 2TB disks and 4 primary partitions. Use 'parted' for GPT partitions.

Question 2: What command creates an XFS filesystem on /dev/sdb1?

  • A) mkfs -t xfs /dev/sdb1
  • B) mkfs.xfs /dev/sdb1
  • C) Both A and B
  • D) xfs_create /dev/sdb1
Answer: C) Both A and B
Both commands create an XFS filesystem. mkfs.xfs is the direct command, while mkfs -t xfs is the generic format. For ext4: mkfs.ext4 or mkfs -t ext4.

Question 3: What field in /etc/fstab determines the fsck check order?

  • A) 4th field (options)
  • B) 5th field (dump)
  • C) 6th field (pass)
  • D) 3rd field (type)
Answer: C) 6th field (pass)
The pass field: 0=no fsck, 1=root filesystem (first), 2=other filesystems (after root). Root should be 1, other filesystems should be 2, swap should be 0.

Question 4: Which is preferred for identifying devices in /etc/fstab?

  • A) Device name (/dev/sdb1)
  • B) UUID
  • C) Label
  • D) Both B and C
Answer: D) Both B and C
UUID and LABEL are preferred over device names because device names can change (e.g., /dev/sdb might become /dev/sdc if hardware is added). UUID is most reliable.

Question 5: How do you test /etc/fstab without rebooting?

  • A) mount -a
  • B) systemctl restart fstab
  • C) fstab --test
  • D) mount --check
Answer: A) mount -a
'mount -a' mounts all filesystems in /etc/fstab. This tests your fstab entries without rebooting. Always run this after editing fstab to catch errors. Also use: findmnt --verify to check fstab syntax.