Managing Disk Partitions with fdisk and parted on CentOS
Partitioning a CentOS disk is a routine administration task, but an incorrect device name or partition boundary can destroy valuable data. The safest approach is to identify the disk carefully, record its current layout, and create a backup before changing the partition table.
CentOS systems commonly use fdisk for interactive partition management and parted for GPT disks, large drives, scripting, and precise alignment. The commands are similar across CentOS 7, CentOS Stream, and related RHEL-based distributions, although available versions may differ.
Australian administrators may manage servers in Sydney or Melbourne data centres while supporting users in Perth or Brisbane. Schedule storage maintenance in an agreed AEST or AEDT window, and consider the Australian Privacy Act when a disk contains customer records, logs, or backups.
Inspecting Disks Before Making Changes
Begin by listing block devices and mounted filesystems:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS,UUID
sudo fdisk -l
df -hT
lsblk shows device relationships, while fdisk -l displays partition-table details. Compare the output with the expected disk size. A device such as /dev/vdb may be a secondary virtual disk, while /dev/sda could contain the operating system. Never assume that the first disk listed is the one you intend to modify.
Check for active mounts, swap, software RAID, or LVM physical volumes before editing:
findmnt
swapon --show
sudo pvs
sudo mdadm --detail --scan
Keep a copy of important configuration and partition information. Notes from shell administration notes can also help when documenting command-line procedures and recovery steps.
Creating a Partition with fdisk
For a straightforward interactive workflow, open the target disk:
sudo fdisk /dev/vdb
Inside fdisk, use p to print the existing table. Press n to create a partition, select the default partition number, and accept the suggested first sector unless a specific layout is required. Enter a size such as +100G, or accept the default final sector to use the remaining free space. Use t only when changing a partition type, then press w to write changes.
Modern fdisk generally creates GPT partition tables on new large disks when appropriate, while older systems may use DOS/MBR. GPT is preferable for disks larger than 2 TiB and for servers requiring many partitions. The g command creates a new GPT table, but it erases the existing partition definitions, so use it only on a confirmed empty disk.
Using parted for GPT and Automation
parted is useful when exact sectors, GPT metadata, or scripted commands are needed:
sudo parted /dev/vdb
(parted) print free
(parted) mklabel gpt
(parted) mkpart primary xfs 1MiB 100%
(parted) print
(parted) quit
The 1MiB starting point provides sensible alignment for current disks and virtual storage. For non-interactive administration, a command can be written as:
sudo parted -s /dev/vdb mkpart primary xfs 1MiB 100%
The filesystem label in mkpart describes the intended use; it does not format the partition. This distinction matters because a newly created partition still needs a filesystem before it can be mounted. Verify the result with lsblk and sudo fdisk -l /dev/vdb.
Formatting and Mounting the New Volume
Only format a partition that contains no required data. For XFS, which is common on CentOS, use:
sudo mkfs.xfs -f /dev/vdb1
sudo mkdir -p /srv/data
sudo mount /dev/vdb1 /srv/data
df -hT /srv/data
For an ext4 filesystem, replace the formatting command with sudo mkfs.ext4 /dev/vdb1. A web server cache, application files, or database data may require particular ownership and mount options. If the volume will support a caching layer, review the Varnish CentOS guide before selecting its directory structure.
Use the UUID rather than /dev/vdb1 in /etc/fstab because device names can change after reboot:
sudo blkid /dev/vdb1
sudo vi /etc/fstab
Add a line similar to:
UUID=your-uuid-here /srv/data xfs defaults 0 0
Test the entry without rebooting:
sudo umount /srv/data
sudo mount -a
findmnt /srv/data
Extending an Existing Partition
When a virtual disk is enlarged, first confirm that the operating system sees the new capacity:
lsblk
sudo fdisk -l /dev/vdb
If the free space follows /dev/vdb1, use parted to extend the partition:
sudo parted /dev/vdb
(parted) resizepart 1 100%
(parted) quit
For an XFS filesystem, grow it while mounted:
sudo xfs_growfs /srv/data
For ext4, use:
sudo resize2fs /dev/vdb1
Partition growth and filesystem growth are separate operations. Expanding the partition alone does not make the additional space available to the filesystem. If the kernel does not detect the changed table, run sudo partprobe /dev/vdb, then confirm the new size with lsblk.
Safe Operations and Troubleshooting
Do not modify a mounted root, boot, or active data partition casually. Stop services, unmount filesystems, and disable swap where necessary. A partition used by LVM or RAID must be managed through the appropriate layer rather than treated as a normal filesystem. Keep console access available when working on a remote server, particularly in an unattended facility.
Common problems include choosing the wrong disk, receiving a “device is busy” message, or finding that mount -a fails after an /etc/fstab edit. Check findmnt, lsof, dmesg, and journalctl -b for evidence. For broader command references, the Linux Unix tutorials provide related administration material.
Before a scheduled change, record the original output of lsblk, fdisk -l, pvs, and df -hT. The practical sequence is simple: identify the device, back up the data, create or extend the partition, verify the table, format only when appropriate, mount by UUID, and test the result before declaring the storage ready.