LVM and Swap#

Concepts#

LVM — Logical Volume Manager#

Regular partitions are rigid — you cannot easily resize them or span them across disks. LVM adds a flexible layer between physical disks and filesystems.

LVM Architecture#

Physical disks:   /dev/sda    /dev/sdb
                      │           │
Physical Volumes: /dev/sda1   /dev/sdb1       ← PV (raw storage)
                      │           │
Volume Group:     ════════ vg_data ═══════    ← VG (pool of storage)
                      │           │
Logical Volumes:  lv_home     lv_var          ← LV (virtual partitions)
                      │           │
Filesystems:      ext4        xfs             ← mounted like regular partitions
  • Physical Volume (PV) — a partition or whole disk assigned to LVM
  • Volume Group (VG) — a pool of storage made from one or more PVs
  • Logical Volume (LV) — a virtual partition carved from a VG (this is what you format and mount)

LVM Advantages#

  • Resize: grow or shrink logical volumes without repartitioning
  • Span disks: combine multiple physical disks into one volume group
  • Snapshots: create point-in-time copies of a volume
  • Move data: migrate data between physical disks without downtime

LVM Commands#

# Install LVM tools
sudo apt install -y lvm2

# --- Physical Volumes ---
sudo pvcreate /dev/sdb1                # initialize a PV
sudo pvs                               # list PVs
sudo pvdisplay                         # detailed PV info

# --- Volume Groups ---
sudo vgcreate vg_data /dev/sdb1        # create a VG from PV(s)
sudo vgextend vg_data /dev/sdc1        # add another PV to the VG
sudo vgs                               # list VGs
sudo vgdisplay                         # detailed VG info

# --- Logical Volumes ---
sudo lvcreate -L 10G -n lv_home vg_data     # create 10GB LV
sudo lvcreate -l 100%FREE -n lv_data vg_data # use all remaining space
sudo lvs                                     # list LVs
sudo lvdisplay                               # detailed LV info

# --- Format and mount ---
sudo mkfs.ext4 /dev/vg_data/lv_home
sudo mount /dev/vg_data/lv_home /home

# --- Resize ---
# Extend a logical volume (add 5GB)
sudo lvextend -L +5G /dev/vg_data/lv_home
# Resize the filesystem to match
sudo resize2fs /dev/vg_data/lv_home    # ext4
# Or for XFS:
# sudo xfs_growfs /mnt/mountpoint

# Extend to fill all free space in VG
sudo lvextend -l +100%FREE /dev/vg_data/lv_home
sudo resize2fs /dev/vg_data/lv_home

Check if You Are Using LVM#

lsblk
# LVM volumes show as "lvm" type:
# sda2       8:2    0  38.5G  0 part
# └─vg--sys-lv--root 253:0 0  38.5G  0 lvm  /

sudo lvs 2>/dev/null
sudo vgs 2>/dev/null

Ubuntu’s server installer and Debian’s guided partitioning can optionally set up LVM during installation.

Swap#

Swap is disk space used as an extension of RAM. When physical RAM is full, the kernel moves less-used memory pages to swap, freeing RAM for active processes.

Swap Types#

Type Description
Swap partition A dedicated partition with type “swap”
Swap file A regular file used as swap (easier to resize)

Managing Swap#

# View current swap
swapon --show
free -h

# --- Swap partition ---
# Already set up during installation on most systems

# --- Swap file (create a 2GB swap file) ---
sudo fallocate -l 2G /swapfile       # allocate space
sudo chmod 600 /swapfile              # restrict permissions
sudo mkswap /swapfile                 # format as swap
sudo swapon /swapfile                 # enable it

# Make it permanent — add to /etc/fstab:
echo "/swapfile  none  swap  sw  0  0" | sudo tee -a /etc/fstab

# --- Disable swap ---
sudo swapoff /swapfile                # disable
sudo rm /swapfile                     # delete the file
# Remove the fstab entry too

Swappiness#

The swappiness parameter controls how aggressively the kernel uses swap:

# Check current value (default: 60)
cat /proc/sys/vm/swappiness

# Temporarily change (0 = avoid swap, 100 = swap aggressively)
sudo sysctl vm.swappiness=10

# Permanent change — add to /etc/sysctl.conf:
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf

A desktop typically benefits from lower swappiness (10-30). The default (60) is fine for servers.

How Much Swap?#

RAM Recommended Swap
≤ 2 GB 2× RAM
2-8 GB Equal to RAM
8-64 GB At least 4 GB (or half of RAM for hibernate)
> 64 GB At least 4 GB

If you use hibernate (suspend to disk), swap must be at least as large as RAM.


Lab#

Exercise 1: Check Your Current Setup#

lsblk
sudo lvs 2>/dev/null
swapon --show
free -h
cat /proc/sys/vm/swappiness

Exercise 2: Create a Swap File#

# Create a 512MB swap file (safe for a VM lab)
sudo fallocate -l 512M /swapfile_test
sudo chmod 600 /swapfile_test
sudo mkswap /swapfile_test
sudo swapon /swapfile_test

# Verify
swapon --show
free -h

# Disable and clean up
sudo swapoff /swapfile_test
sudo rm /swapfile_test

Exercise 3: Explore LVM (Read Only)#

# If your system uses LVM:
sudo pvs 2>/dev/null
sudo vgs 2>/dev/null
sudo lvs 2>/dev/null
sudo lvdisplay 2>/dev/null | head -20

# If not using LVM, that's OK — review the concepts

Review#

1. What are the three layers of LVM?

Physical Volumes (PVs) — raw storage. Volume Groups (VGs) — pools of PVs. Logical Volumes (LVs) — virtual partitions carved from VGs that you format and mount.

2. What is the main advantage of LVM over regular partitions?

Flexibility. You can resize logical volumes without repartitioning, span multiple disks, and take snapshots. Regular partitions are rigid and hard to resize.

3. What is swap?

Disk space used as an extension of RAM. When physical memory is full, the kernel moves less-used data to swap to free up RAM. It can be a dedicated partition or a file.

4. How do you create a swap file?

fallocate -l SIZE /swapfile, chmod 600 /swapfile, mkswap /swapfile, swapon /swapfile. Add to /etc/fstab for persistence.

5. What does swappiness control?

How aggressively the kernel uses swap. 0 = avoid swap (use RAM as much as possible). 100 = swap aggressively. Default is 60. Desktop users often lower it to 10-30.


Previous: Filesystems and Mounting | Next: SSH Fundamentals