Disks and Partitioning
Disk and partition management is a core skill for Linux system administrators. This article starts with viewing disk information and covers MBR/GPT partition creation, filesystem formatting, mount operations, and automatic mounting at boot via /etc/fstab.
Viewing Disk and Block Device Information
Section titled “Viewing Disk and Block Device Information”Viewing Block Devices with lsblk
Section titled “Viewing Block Devices with lsblk”lsblk lists all block devices and their mount points in a tree structure:
lsblkExample output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTSsda 8:0 0 50G 0 disk├─sda1 8:1 0 1G 0 part /boot└─sda2 8:2 0 49G 0 part ├─rl-root 253:0 0 45G 0 lvm / └─rl-swap 253:1 0 4G 0 lvm [SWAP]sdb 8:16 0 100G 0 diskFor more detailed information including filesystem type and UUID:
lsblk -fListing the Partition Table with fdisk
Section titled “Listing the Partition Table with fdisk”sudo fdisk -l /dev/sdbViewing UUIDs with blkid
Section titled “Viewing UUIDs with blkid”sudo blkidMBR Partitioning: Using fdisk
Section titled “MBR Partitioning: Using fdisk”fdisk is used to manage MBR (Master Boot Record) partition tables, supporting a maximum of 4 primary partitions or 3 primary partitions plus 1 extended partition. MBR supports disks up to 2TB.
-
Start fdisk in interactive mode:
Terminal window sudo fdisk /dev/sdb -
Create a new partition in the interactive interface:
Command (m for help): nPartition typep primary (0 primary, 0 extended, 4 free)e extended (container for logical partitions)Select (default p): pPartition number (1-4, default 1): 1First sector (2048-209715199, default 2048): Press EnterLast sector ... (default 209715199): +20G -
View the created partitions:
Command (m for help): p -
Write the partition table and exit:
Command (m for help): w
Common fdisk Interactive Commands
Section titled “Common fdisk Interactive Commands”| Command | Description |
|---|---|
n | Create a new partition |
d | Delete a partition |
p | Print the partition table |
t | Change the partition type |
l | List known partition types |
w | Write changes and exit |
q | Quit without saving |
GPT Partitioning: Using gdisk
Section titled “GPT Partitioning: Using gdisk”GPT (GUID Partition Table) overcomes the limitations of MBR, supporting disks larger than 2TB and up to 128 partitions. GPT is strongly recommended for modern servers.
-
Install gdisk (if not already installed):
Terminal window sudo dnf install gdisk -y -
Start gdisk in interactive mode:
Terminal window sudo gdisk /dev/sdb -
Create a new GPT partition:
Command (? for help): nPartition number (1-128, default 1): 1First sector (34-209715166, default = 2048): Press EnterLast sector ... (default = 209715166): +50GCurrent type is 8300 (Linux filesystem)Hex code or GUID (L to show codes, Enter = 8300): Press Enter -
View the partition layout:
Command (? for help): p -
Write and exit:
Command (? for help): w
gdisk vs. fdisk Comparison
Section titled “gdisk vs. fdisk Comparison”| Feature | fdisk (MBR) | gdisk (GPT) |
|---|---|---|
| Maximum disk capacity | 2TB | 9.4ZB |
| Maximum number of partitions | 4 primary | 128 partitions |
| Partition table backup | None | One copy at each end of the disk |
| Use case | Legacy system compatibility | Recommended for modern servers |
Notifying the Kernel of Partition Table Updates: partprobe
Section titled “Notifying the Kernel of Partition Table Updates: partprobe”After creating or modifying partitions, if the system does not automatically recognize the new partition table, use partprobe to notify the kernel to re-read the partition information:
sudo partprobe /dev/sdbVerify that the kernel has recognized the new partitions:
lsblk /dev/sdbcat /proc/partitions | grep sdbCreating Filesystems: mkfs
Section titled “Creating Filesystems: mkfs”After creating partitions, they need to be formatted before use.
Creating an XFS Filesystem (RHEL/CentOS default)
Section titled “Creating an XFS Filesystem (RHEL/CentOS default)”sudo mkfs.xfs /dev/sdb1Force overwrite an existing filesystem:
sudo mkfs.xfs -f /dev/sdb1Creating an EXT4 Filesystem
Section titled “Creating an EXT4 Filesystem”sudo mkfs.ext4 /dev/sdb1Creating a Filesystem with a Label
Section titled “Creating a Filesystem with a Label”# XFS with labelsudo mkfs.xfs -L mydata /dev/sdb1
# EXT4 with labelsudo mkfs.ext4 -L mydata /dev/sdb1Creating a Swap Partition
Section titled “Creating a Swap Partition”sudo mkswap /dev/sdb2sudo swapon /dev/sdb2Mounting and Unmounting
Section titled “Mounting and Unmounting”Temporary Mount
Section titled “Temporary Mount”Create a mount point and mount the partition:
sudo mkdir -p /mnt/datasudo mount /dev/sdb1 /mnt/dataMount using UUID (recommended):
sudo mount UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" /mnt/dataView current mount status:
mount | grep sdbdf -hT /mnt/dataUnmounting a Filesystem
Section titled “Unmounting a Filesystem”sudo umount /mnt/dataIf the device is reported as busy, find the processes using it:
sudo lsof +D /mnt/datasudo fuser -mv /mnt/dataLazy unmount (automatically unmounts after all users release it):
sudo umount -l /mnt/dataPersistent Mounting: /etc/fstab
Section titled “Persistent Mounting: /etc/fstab”Temporary mounts are lost after a system reboot. To mount automatically at boot, edit /etc/fstab.
Getting the Partition UUID
Section titled “Getting the Partition UUID”sudo blkid /dev/sdb1Example output:
/dev/sdb1: UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" TYPE="xfs"Editing /etc/fstab
Section titled “Editing /etc/fstab”# Always back up firstsudo cp /etc/fstab /etc/fstab.baksudo vi /etc/fstabAdd the following line:
UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 /mnt/data xfs defaults 0 0fstab Field Descriptions
Section titled “fstab Field Descriptions”| Field | Description | Example |
|---|---|---|
| Device identifier | UUID or device path | UUID=xxxx |
| Mount point | Target directory for mounting | /mnt/data |
| Filesystem type | xfs, ext4, swap, etc. | xfs |
| Mount options | Comma-separated list of options | defaults |
| dump backup | 0 means do not use dump backup | 0 |
| fsck check order | 0 = no check, 1 = root partition, 2 = other | 0 |
Common Mount Options
Section titled “Common Mount Options”| Option | Description |
|---|---|
defaults | Equivalent to rw,suid,dev,exec,auto,nouser,async |
noexec | Prevent execution of binaries |
nosuid | Prevent SUID/SGID bits from taking effect |
nodev | Prevent interpretation of character or block devices |
ro | Mount as read-only |
noatime | Do not update access times; improves I/O performance |
nofail | Do not block system boot if the device is absent |
Verifying fstab Configuration
Section titled “Verifying fstab Configuration”Test all fstab entries without rebooting:
sudo mount -aCheck whether the mount was successful:
df -hT /mnt/dataBest Practices for Using UUIDs
Section titled “Best Practices for Using UUIDs”Device names (such as /dev/sdb1) can change when hard drives are added or removed. UUIDs are unique partition identifiers that are not affected by device ordering.
Multiple Ways to View UUIDs
Section titled “Multiple Ways to View UUIDs”# Method 1: blkidsudo blkid
# Method 2: lsblk -flsblk -f
# Method 3: View by-uuid symlinksls -l /dev/disk/by-uuid/Recommended fstab Format
Section titled “Recommended fstab Format”# Recommended: Use UUID with nofail to prevent boot failuresUUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 /mnt/data xfs defaults,nofail 0 0
# Not recommended: Using device names, which may change/dev/sdb1 /mnt/data xfs defaults 0 0Hands-On: Partitioning a New Disk and Configuring Auto-Mount
Section titled “Hands-On: Partitioning a New Disk and Configuring Auto-Mount”The following demonstrates the complete process of creating a GPT partition on a brand new 100G disk /dev/sdb, formatting it, and configuring automatic mounting.
-
Confirm the disk status:
Terminal window lsblksudo fdisk -l /dev/sdb -
Create a GPT partition with gdisk:
Terminal window sudo gdisk /dev/sdb# Interactive steps: n -> 1 -> Enter -> +80G -> Enter -> w -> Y -
Notify the kernel to recognize the new partition:
Terminal window sudo partprobe /dev/sdblsblk /dev/sdb -
Format as an XFS filesystem:
Terminal window sudo mkfs.xfs /dev/sdb1 -
Create a mount point and temporarily mount:
Terminal window sudo mkdir -p /datasudo mount /dev/sdb1 /datadf -hT /data -
Get the UUID and configure fstab:
Terminal window # Get the UUIDUUID=$(sudo blkid -s UUID -o value /dev/sdb1)echo "UUID: $UUID"# Back up fstabsudo cp /etc/fstab /etc/fstab.bak# Write to fstabecho "UUID=$UUID /data xfs defaults,nofail 0 0" | sudo tee -a /etc/fstab -
Verify the auto-mount configuration:
Terminal window sudo umount /datasudo mount -adf -hT /data
With this, the disk partitioning and mount configuration are complete. The /data directory will be automatically mounted after a system reboot.