How to Format and Mount a New Disk on Linux

Find a newly attached volume, partition and format it with ext4 or XFS, mount it, make it permanent in fstab by UUID, and move a directory onto it safely.

Published
Reading time
3 min

Adding a volume to a VPS — for uploads, backups or a database that outgrew the root disk — takes four commands and one line in a file. The one line is where people get it wrong and end up with a server that will not boot. Here is the sequence, with the safe way to write the fstab entry.

1. Find the new disk

bash
lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0   80G  0 disk
└─vda1 253:1    0   80G  0 part /
vdb    253:16   0  200G  0 disk

vdb with no partitions and no mountpoint is the new one. Names vary: sdb on some hypervisors, nvme1n1 on NVMe, xvdf on AWS. Confirm with sudo fdisk -l /dev/vdb — it should show no existing partition table. Formatting the wrong device destroys its data with no undo; check twice.

2. Partition (optional but conventional)

A single partition using the whole disk:

bash
sudo parted /dev/vdb --script mklabel gpt mkpart primary ext4 0% 100%
lsblk /dev/vdb

You can format the raw device without a partition table and it works fine; a partition table just makes the disk's purpose obvious to the next admin and to tools.

3. Format

ext4 — the default Linux filesystem; reliable, well understood, can be shrunk. XFS — the default on AlmaLinux/Rocky; excellent for large files and parallel I/O, cannot be shrunk. For a web server's uploads or backups, either is right.

bash
sudo mkfs.ext4 -L data /dev/vdb1
# or
sudo mkfs.xfs -L data /dev/vdb1

-L data sets a label you can mount by.

4. Mount

bash
sudo mkdir -p /mnt/data
sudo mount /dev/vdb1 /mnt/data
df -h /mnt/data

5. Make it permanent — by UUID

Device names like /dev/vdb can change between boots when disks are added or removed. The UUID does not. Get it:

bash
sudo blkid /dev/vdb1
# /dev/vdb1: LABEL="data" UUID="3f8a1c2e-...." TYPE="ext4"

Add to /etc/fstab:

UUID=3f8a1c2e-....  /mnt/data  ext4  defaults,nofail  0  2

nofail is the important word: if the disk is missing at boot, the system boots anyway instead of dropping to an emergency shell. For XFS, the type is xfs and the last number 0.

Test the line before rebooting:

bash
sudo umount /mnt/data
sudo mount -a
df -h /mnt/data

If mount -a errors, fix the fstab line now, while you still have a running system. On systemd, also run sudo systemctl daemon-reload so the generated mount unit updates.

6. Move a directory onto the new disk

To move, say, a site's uploads without changing any application config, copy the data, then bind-mount or symlink:

bash
sudo rsync -a /var/www/example.com/httpdocs/wp-content/uploads/ /mnt/data/uploads/
sudo mv /var/www/example.com/httpdocs/wp-content/uploads /var/www/example.com/httpdocs/wp-content/uploads.old
sudo ln -s /mnt/data/uploads /var/www/example.com/httpdocs/wp-content/uploads

Check ownership on the new location matches (chown -R user:group), load the site, then delete uploads.old once you are sure. For MySQL's data directory, stop the service first, rsync -a with the directory ending in /, update datadir in the config (and the AppArmor/SELinux policy for the new path), then start — test on a copy first; it is the one move that can take a database down if the permissions are wrong.

Growing a disk later

If the provider enlarges the volume in place, the filesystem does not grow automatically:

bash
sudo growpart /dev/vdb 1          # extend the partition (cloud-guest-utils)
sudo resize2fs /dev/vdb1          # ext4, online
sudo xfs_growfs /mnt/data         # XFS, online, by mountpoint

Both work while mounted.

Checking health

bash
sudo smartctl -a /dev/vdb | grep -iE "health|reallocated|pending"    # physical disks
sudo tune2fs -l /dev/vdb1 | grep -i "mount count\|last checked"    # ext4 fsck history

On a VPS, the disk is virtual and its health is the host's concern. On a VPSPioneer managed VPS, extra volumes are attached, formatted and mounted by us on request, with the fstab written the safe way above, and they are included in the nightly backups by default.

#linux#disk#storage#fstab#mount

Keep reading

More from Linux

All guides

Linux

How to Check Disk Space on Linux and Find What Is Filling It

df, du and ncdu to see where the space went, the usual culprits on a web server — logs, backups, caches, mail — and how to clear them safely.

3 min read →

Linux

Linux Log Files: Where They Are and How to Read Them

Where each log lives on Ubuntu and AlmaLinux — system, auth, web, PHP, MySQL, mail, Plesk — how to read them fast, and what to look for when something breaks.

3 min read →

Linux

How to Manage Users and Groups on Linux

Create, modify, lock and remove user accounts, use groups to share access safely, set up a deploy user for a site, and audit who can log in to a server.

3 min read →