Storage Management in RHEL/CentOS made Simpler (CLI Tools)

Storage Management in RHEL/CentOS made Simpler (CLI Tools)

RHEL and CentOS provide powerful command-line tools for disk and storage management. To get started, identify existing disks and partitions using commands like lsblk (list block devices) or fdisk -l. For example, lsblk displays all disks and partitions in a clear tree view. The fdisk -l command reports disk sizes and partition tables; e.g., it might show a 30 GiB /dev/sda disk with /dev/sda2 marked as an LVM partition.

Partitioning Disks with fdisk

To create or modify disk partitions on a raw disk, use fdisk (or parted for GPT, though we focus on fdisk). For example, list current partitions on /dev/sdb with:

# fdisk -l /dev/sdb
Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 sectors
...
Device    Boot Start    End   Sectors  Size Id Type

Then run fdisk /dev/sdb to enter interactive mode. Use the n command to add a new partition. You will be prompted for primary (p) or extended (e) type, partition number, and size. For instance:

# fdisk /dev/sdb
Command (m for help): n
Partition type:   p  # primary
Partition number: 1
First sector:  (accept default)
Last sector, +size: +1G
Command (m for help): w  # write changes

This creates a 1 GiB partition /dev/sdb1 and saves the changes. (If you make a mistake, you can delete a partition with d before writing.) After writing, verify with lsblk or fdisk -l. Finally, format the new partition (for example as ext4) and mount it:

# mkfs.ext4 /dev/sdb1
# mkdir /mnt/data && mount /dev/sdb1 /mnt/data

This workflow is common in production: identify a disk, create the needed partition, format it, and mount it for use.

Logical Volume Management (LVM)

LVM adds a layer of abstraction over physical disks, making volume resizing and management easier. In LVM, you first designate one or more block devices (or partitions) as Physical Volumes (PV), which are pooled into a Volume Group (VG), from which you carve out Logical Volumes (LV). For example, initialize two new disks as PVs:

# pvcreate /dev/sdb /dev/sdc

This labels them for LVM use. Next create a volume group, say vgdata, spanning these PVs:

# vgcreate vgdata /dev/sdb /dev/sdc

This produces a new VG named vgdata. Check with pvs and vgs; e.g., vgs will report the VG’s total and free space. Now create a logical volume. For a 10 GiB data volume named lv_data:

# lvcreate -L 10G -n lv_data vgdata

This makes /dev/vgdata/lv_data. Finally, format and mount the LV as you would a partition:

# mkfs.xfs /dev/vgdata/lv_data
# mkdir /data && mount /dev/vgdata/lv_data /data

Now /data uses the LVM volume. You can expand it later with lvextend and xfs_growfs (for XFS) without downtime. Throughout, use pvsvgs, and lvs to inspect LVM status – for example, vgs will show the free space available in your VG. Using one partition (or one whole disk) per PV is recommended for simplicity and performance.

Swap Space Configuration

Swap provides “overflow” virtual memory to prevent out-of-memory errors, though heavy swap use can degrade performance. On RHEL/CentOS you can use either a swap partition or a swap file. To create a swap partition (e.g. /dev/sdc1), first set the type to swap in fdisk, then run:

# mkswap /dev/sdc1
# swapon /dev/sdc1

This marks /dev/sdc1 as swap space. (Add a line in /etc/fstab like /dev/sdc1 none swap defaults 0 0 for persistence.) To verify, use swapon --show or inspect /proc/swaps – for example, cat /proc/swaps will list active swap devices and sizes. The free -h command also reports total swap in use.

Alternatively, create a swap file. For a 2 GiB swap file, you might run:

# dd if=/dev/zero of=/swapfile bs=1M count=2048
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile

This sequence allocates a 2GB file, secures it, formats it as swap, and activates it (a similar example is shown in Red Hat docs). Remember to add /swapfile none swap defaults 0 0 to /etc/fstab so it activates at boot.

LVM Extension

LVM allows flexible disk space management by abstracting physical storage into logical volumes. Extending an LVM volume typically involves:

  1. Adding a new physical volume (PV),
  2. Extending the volume group (VG),
  3. Resizing the logical volume (LV),
  4. Resizing the filesystem.

Step-by-Step: Extend LVM in RHEL/CentOS

1. Identify New Disk (Optional)

If you’re adding a new disk:

lsblk

Assume the new disk is /dev/sdb.

2. Create Physical Volume

sudo pvcreate /dev/sdb

3. Extend Volume Group

Find your volume group name:

vgs

Extend the VG (replace vg_name with your actual VG name):

sudo vgextend vg_name /dev/sdb

4. Extend Logical Volume

List your logical volumes:

lvdisplay

Then extend it (replace lv_path accordingly):

sudo lvextend -l +100%FREE /dev/vg_name/lv_name

This command adds all available VG space to the logical volume.

5. Resize the Filesystem

Depending on the filesystem type:

For ext4:

sudo resize2fs /dev/vg_name/lv_name

For xfs:

sudo xfs_growfs /mount/point

Example

pvcreate /dev/sdb
vgextend centos /dev/sdb
lvextend -l +100%FREE /dev/centos/root
xfs_growfs /

# To extend volume with 10 Gb  space; extending the filesystem with -r
lvextend -L 50G -r /dev/vg_name/lv_name

Tips

  • To shrink LVM volumes, you must shrink the filesystem first before reducing the logical volume.
  • Use df -h to monitor disk usage before and after extension.
  • You can also extend LVM via tools like cockpit if GUI is available.

Monitoring swap and usage is straightforward: free -h shows how much swap is configured and used, helping you tune the size or add more if needed. In real-world practice, sysadmins often combine these techniques – for example, extending an existing swap LV (with lvextend) or quickly adding a swap file on low-memory systems.

Each of these CLI tools – lsblkfdiskpvcreatevgcreatelvcreatemkswapswapon, etc. – is essential for RHEL/CentOS storage tasks. By chaining them in scripts or as needed, you can automate disk provisioning: partition disks, join them to LVM, and manage swap, all without a GUI. This makes the system more flexible and easier to adapt as storage needs change.

Posts Carousel

Leave a Comment

Your email address will not be published. Required fields are marked with *

1 Comment

  • Renney
    June 9, 2025, 4:55 pm

    Thanks for this. Simple and clear

    REPLY

Latest Posts

Most Commented

Featured Videos