Linux·7 min read·

Understanding Swap, Partitions, and LVM on a VPS

How swap space, disk partitions, and LVM logical volumes work together to manage storage on a Linux VPS.

NB

Netbay Engineering

Netbay Engineering

On this page

Storage on a Linux VPS is not a single flat drive. It is a layered system made of physical disks, partitions, logical volumes, filesystems, and swap space. Understanding these layers helps you diagnose low-disk warnings, plan for growth, and recover from out-of-space incidents. This guide covers swap, partitions, and LVM, the three foundational storage concepts.

Swap Space: Virtual Memory Extension

Swap is disk space used as an overflow for RAM. When memory is exhausted, the kernel moves less-active pages to swap, freeing physical RAM for active workloads. A sudden process spike that needs more memory than you have can be absorbed by swap instead of triggering the out-of-memory killer.

On a typical VPS, you configure swap as either a swap file or a swap partition. The modern approach is a swap file because it is easier to resize:

bash
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap sw 0 0" >> /etc/fstab

Check how much swap is in use and its current status:

bash
free -h
swapon --show
vmstat 1 5

A healthy system should rarely touch swap beyond a small amount. If swap usage climbs and stays high, your workload may need more RAM. Monitoring free -h over time tells you whether to plan an upgrade.

Disk Partitions

Physical or virtual disks are divided into partitions. On a modern VPS with KVM virtualization, the operating system is typically installed on a single virtual disk that is partitioned with GPT. You can inspect the layout with lsblk:

bash
lsblk
lsblk -f
sudo fdisk -l /dev/vda

lsblk -f shows the filesystem type and mount point for each partition. If you see a partition like /dev/vda1 mounted at /boot and /dev/vda2 mounted at /, you are looking at a standard two-partition layout.

For most VPS workloads a single root partition plus a separate swap area is sufficient. Mounting /tmp as a separate partition with the noexec option is a lightweight hardening technique because it prevents executing code from the temporary directory.

LVM: Logical Volume Management

LVM abstracts physical storage into logical volumes. Instead of a fixed partition, you create physical volumes, group them into a volume group, and divide the group into logical volumes. The benefit is flexibility: you can extend a logical volume without repartitioning the disk.

Typical LVM commands:

bash
pvcreate /dev/sdb
vgextend vg00 /dev/sdb
lvextend -L +20G /dev/vg00/lv_data
resize2fs /dev/vg00/lv_data

The first command initializes a new physical disk for LVM. The second expands the volume group. The third grows a logical volume by 20 gigabytes, and the fourth grows the ext4 filesystem to match the new size.

Inspect your LVM layout:

bash
pvs
vgs
lvs
df -h
mount | grep dm-

The pvs, vgs, and lvs commands summarize physical volumes, volume groups, and logical volumes respectively.

Resizing a Logical Volume Safely

Growing a filesystem is the most common LVM operation. The order matters: first grow the logical volume, then grow the filesystem:

bash
lvextend -L 50G /dev/vg00/lv_data
resize2fs /dev/vg00/lv_data
df -h /data

Shrinking is riskier and requires unmounting the filesystem, shrinking it first, and then shrinking the logical volume. For production data, prefer adding a new disk and expanding rather than shrinking.

Choosing Between Partitions and LVM

  • Plain partitions are simple, predictable, and work everywhere, but resizing requires repartitioning.
  • LVM adds indirection and flexibility at the cost of some complexity.
  • Most Netbay Linux VPS images ship with a convenient partition layout managed by your distribution's installer. For advanced storage needs, LVM is the standard tool.
Storage Layers on a Linux VPS Physical / Virtual Disk (High-Speed SSD) Partition 1 /boot Partition 2 LVM PV Volume Group vg00 pool of storage Swap file memory overflow LV root mount / LV data mount /var/www lvextend + resize2fs to grow storage

Takeaway

Swap smooths memory pressure, partitions organize the disk, and LVM gives you flexible, resizable storage. Understand these layers and out-of-space panics become routine fixes. Practice storage administration on a Linux VPS with Netbay at netbayhosts.in.

Keep reading

Follow along on a real VPS

Deploy Linux in under 60 seconds

These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.

Deploy an instance