Skip to content

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.

lsblk lists all block devices and their mount points in a tree structure:

Terminal window
lsblk

Example output:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 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 disk

For more detailed information including filesystem type and UUID:

Terminal window
lsblk -f
Terminal window
sudo fdisk -l /dev/sdb
Terminal window
sudo blkid

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.

  1. Start fdisk in interactive mode:

    Terminal window
    sudo fdisk /dev/sdb
  2. Create a new partition in the interactive interface:

    Command (m for help): n
    Partition type
    p primary (0 primary, 0 extended, 4 free)
    e extended (container for logical partitions)
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-209715199, default 2048): Press Enter
    Last sector ... (default 209715199): +20G
  3. View the created partitions:

    Command (m for help): p
  4. Write the partition table and exit:

    Command (m for help): w
CommandDescription
nCreate a new partition
dDelete a partition
pPrint the partition table
tChange the partition type
lList known partition types
wWrite changes and exit
qQuit without saving

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.

  1. Install gdisk (if not already installed):

    Terminal window
    sudo dnf install gdisk -y
  2. Start gdisk in interactive mode:

    Terminal window
    sudo gdisk /dev/sdb
  3. Create a new GPT partition:

    Command (? for help): n
    Partition number (1-128, default 1): 1
    First sector (34-209715166, default = 2048): Press Enter
    Last sector ... (default = 209715166): +50G
    Current type is 8300 (Linux filesystem)
    Hex code or GUID (L to show codes, Enter = 8300): Press Enter
  4. View the partition layout:

    Command (? for help): p
  5. Write and exit:

    Command (? for help): w
Featurefdisk (MBR)gdisk (GPT)
Maximum disk capacity2TB9.4ZB
Maximum number of partitions4 primary128 partitions
Partition table backupNoneOne copy at each end of the disk
Use caseLegacy system compatibilityRecommended 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:

Terminal window
sudo partprobe /dev/sdb

Verify that the kernel has recognized the new partitions:

Terminal window
lsblk /dev/sdb
cat /proc/partitions | grep sdb

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)”
Terminal window
sudo mkfs.xfs /dev/sdb1

Force overwrite an existing filesystem:

Terminal window
sudo mkfs.xfs -f /dev/sdb1
Terminal window
sudo mkfs.ext4 /dev/sdb1
Terminal window
# XFS with label
sudo mkfs.xfs -L mydata /dev/sdb1
# EXT4 with label
sudo mkfs.ext4 -L mydata /dev/sdb1
Terminal window
sudo mkswap /dev/sdb2
sudo swapon /dev/sdb2

Create a mount point and mount the partition:

Terminal window
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data

Mount using UUID (recommended):

Terminal window
sudo mount UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" /mnt/data

View current mount status:

Terminal window
mount | grep sdb
df -hT /mnt/data
Terminal window
sudo umount /mnt/data

If the device is reported as busy, find the processes using it:

Terminal window
sudo lsof +D /mnt/data
sudo fuser -mv /mnt/data

Lazy unmount (automatically unmounts after all users release it):

Terminal window
sudo umount -l /mnt/data

Temporary mounts are lost after a system reboot. To mount automatically at boot, edit /etc/fstab.

Terminal window
sudo blkid /dev/sdb1

Example output:

/dev/sdb1: UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" TYPE="xfs"
Terminal window
# Always back up first
sudo cp /etc/fstab /etc/fstab.bak
sudo vi /etc/fstab

Add the following line:

UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 /mnt/data xfs defaults 0 0
FieldDescriptionExample
Device identifierUUID or device pathUUID=xxxx
Mount pointTarget directory for mounting/mnt/data
Filesystem typexfs, ext4, swap, etc.xfs
Mount optionsComma-separated list of optionsdefaults
dump backup0 means do not use dump backup0
fsck check order0 = no check, 1 = root partition, 2 = other0
OptionDescription
defaultsEquivalent to rw,suid,dev,exec,auto,nouser,async
noexecPrevent execution of binaries
nosuidPrevent SUID/SGID bits from taking effect
nodevPrevent interpretation of character or block devices
roMount as read-only
noatimeDo not update access times; improves I/O performance
nofailDo not block system boot if the device is absent

Test all fstab entries without rebooting:

Terminal window
sudo mount -a

Check whether the mount was successful:

Terminal window
df -hT /mnt/data

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.

Terminal window
# Method 1: blkid
sudo blkid
# Method 2: lsblk -f
lsblk -f
# Method 3: View by-uuid symlinks
ls -l /dev/disk/by-uuid/
Terminal window
# Recommended: Use UUID with nofail to prevent boot failures
UUID=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 0

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

  1. Confirm the disk status:

    Terminal window
    lsblk
    sudo fdisk -l /dev/sdb
  2. Create a GPT partition with gdisk:

    Terminal window
    sudo gdisk /dev/sdb
    # Interactive steps: n -> 1 -> Enter -> +80G -> Enter -> w -> Y
  3. Notify the kernel to recognize the new partition:

    Terminal window
    sudo partprobe /dev/sdb
    lsblk /dev/sdb
  4. Format as an XFS filesystem:

    Terminal window
    sudo mkfs.xfs /dev/sdb1
  5. Create a mount point and temporarily mount:

    Terminal window
    sudo mkdir -p /data
    sudo mount /dev/sdb1 /data
    df -hT /data
  6. Get the UUID and configure fstab:

    Terminal window
    # Get the UUID
    UUID=$(sudo blkid -s UUID -o value /dev/sdb1)
    echo "UUID: $UUID"
    # Back up fstab
    sudo cp /etc/fstab /etc/fstab.bak
    # Write to fstab
    echo "UUID=$UUID /data xfs defaults,nofail 0 0" | sudo tee -a /etc/fstab
  7. Verify the auto-mount configuration:

    Terminal window
    sudo umount /data
    sudo mount -a
    df -hT /data

With this, the disk partitioning and mount configuration are complete. The /data directory will be automatically mounted after a system reboot.