Cyber Services / All lessons / CentOS / Linux Administration / Adding Partitions & File Systems

Adding Partitions & File Systems

Module 17 of 33 · 4 min read · Free to read

MODULES ↓ Command blocks have a COPY button

The Administrator's Guide to Expanding Server Storage

A server's storage is not infinite. As data grows, a system administrator's most common tasks is adding a new disk, preparing it for use, and making it permanently available to the operating system. This is a foundational skill that requires understanding the entire storage lifecycle, from the raw hardware to the accessible directory. This module provides a deep, step-by-step guide to this essential process on a CentOS/RHEL server.

The Storage Lifecycle: A Four-Phase Process

Think of adding a new hard drive like building a new wing on a library. You can't just start putting books in an empty plot of land. You must follow a structured process.

  1. Plan & Identify

    You must first identify the raw, empty plot of land. This is the new, unformatted block device.

  2. Partition the Disk

    You lay the foundation and draw the blueprints for the rooms. This divides the raw disk into usable segments called partitions.

  3. Create a File System

    You build the actual library, installing shelves, catalogs, and a filing system. This is formatting the partition with a file system like XFS.

  4. Mount Persistently

    You connect the new wing to the main library with a hallway and add it to the official library map. This is mounting the file system to a directory and adding it to /etc/fstab.

Phase 1: Identifying the New Disk

After physically adding a new virtual or physical disk to your server, you must identify its device name. The lsblk command provides a clear tree view of all block devices.

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
└─sda2   8:2    0   49G  0 part /
sdb      8:16   0   20G  0 disk      <-- OUR NEW DISK
sr0     11:0    1 1024M  0 rom 

In this output, /dev/sda is our primary disk with the OS on it. The new, 20GB disk has been identified as /dev/sdb. It has no partitions and no mount point. This is our target.

Phase 2: Partitioning the Disk with `fdisk`

Partitioning divides a disk into one or more logical sections. We will create one single partition that uses the entire 20GB disk. We use the interactive fdisk utility.

Destructive Operation Warning

Partitioning is a destructive process. Be absolutely certain you are targeting the correct disk (/dev/sdb, not /dev/sda). A mistake here can wipe out your entire operating system.

# Start the fdisk utility for the target disk
$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):

You are now inside the `fdisk` prompt. You will enter a series of single-letter commands:

  1. n - Create a new partition.
  2. p - Choose a primary partition.
  3. 1 - Make it partition number 1.
  4. Enter - Accept the default first sector.
  5. Enter - Accept the default last sector (this uses the entire disk).
  6. p - Print the new partition table to verify your work. You should see a new device listed as /dev/sdb1.
  7. w - Write the changes to the disk and exit. This is the final, irreversible step.

Phase 3: Creating a File System (Formatting)

Our new partition, /dev/sdb1, is just a defined space. Now we must format it with a file system to make it usable. The standard on CentOS/RHEL is XFS.

# Use the 'make filesystem' command for XFS
$ sudo mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1310656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=5242624, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
Done

Phase 4: Mounting the File System Persistently

The final phase is to make this new, formatted filesystem available to users and ensure it's there after every reboot.

The `fstab` Workflow

  1. Create a Mount Point: This is the empty directory where the filesystem will live.
    $ sudo mkdir /data
  2. Find the Device's Unique ID (UUID): Device names like /dev/sdb1 can sometimes change on reboot. The UUID is a permanent, unique identifier that will never change. This is the safest way to identify your partition.
    $ sudo blkid /dev/sdb1
    /dev/sdb1: UUID="a1b2c3d4-e5f6-7890-1234-567890abcdef" TYPE="xfs" ...
    Copy the long string inside the `UUID="..."` quotes.
  3. Edit `/etc/fstab`: This is the critical step. Back it up first: sudo cp /etc/fstab /etc/fstab.bak. Then open the file with sudo vim /etc/fstab and add this new line at the very bottom, pasting your UUID.
    #                                      
    UUID="a1b2c3d4-e5f6-7890-1234-567890abcdef"  /data         xfs    defaults   0      0
    
  4. Test and Mount: The mount -a command will read the `/etc/fstab` file and attempt to mount anything that isn't already mounted. This is the perfect way to test your new line for syntax errors without rebooting.
    $ sudo mount -a
    If this command returns no errors, your `fstab` entry is correct.
  5. Final Verification: Use lsblk or df -h (Disk Free) to confirm that your device is now persistently mounted.
    $ df -h /data
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sdb1        20G   33M   20G   1% /data
Modules (33) ← CentOS / Linux Administration