Skip to content

Intermediate · Storage: partitions, file systems and LVM · topic 28 of 57 · 35 min

Growing storage and adding swap

Explanation

Grow an LV and its file system in one step with lvextend -r. If the VG is out of space, add a disk with vgextend first. Swap is disk space used as overflow memory: make it with mkswap, turn it on with swapon, and add a swap line to /etc/fstab.

Growing a logical volume while it is in use

The main reason to use LVM is that you can make a logical volume bigger while it is mounted and in use, with no downtime. Two things must grow: the LV (the space) and the file system on it (so it can use the space). lvextend -r (--resizefs) does both in one command.

bash
# df -h /data
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vgdata-lvdata  320M  290M   30M  91% /data
# vgs vgdata
  VG     #PV #LV #SN Attr   VSize VFree
  vgdata   2   1   0 wz--n- 6.97g 6.66g
# lvextend -r -L +1G /dev/vgdata/lvdata        # ADD 1 GiB (+ means add)
  Size of logical volume vgdata/lvdata changed from 320.00 MiB (20 extents) to <1.32 GiB (84 extents).
  Logical volume vgdata/lvdata successfully resized.
meta-data=/dev/mapper/vgdata-lvdata ...
data blocks changed from 81920 to 344064
# df -h /data
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vgdata-lvdata  1.4G  300M  1.1G  22% /data
CommandMeaning
lvextend -L +1GAdd 1 GiB to the current size
lvextend -L 5GMake the total size 5 GiB
lvextend -l +50Add 50 extents
lvextend -l +100%FREEAdd all free space in the VG

If you forget -r, the LV grows but df still shows the old size. Then grow the file system by hand: xfs_growfs /data for XFS (it takes the MOUNT POINT), or resize2fs /dev/vgdata/lvdata for ext4 (it takes the device).

When the volume group is full: vgextend

bash
# pvcreate /dev/vdd
# vgextend vgdata /dev/vdd
  Volume group "vgdata" successfully extended
# vgs vgdata
  VG     #PV #LV #SN Attr   VSize  VFree
  vgdata   3   1   0 wz--n- 11.96g 10.64g
# lvextend -r -l +100%FREE /dev/vgdata/lvdata
Add a new PV to the group, and the LV can grow into it.
Careful: Shrinking is dangerous. XFS cannot shrink at all. ext4 can, but only unmounted and only if you shrink the file system BEFORE the LV (lvreduce -r does both in the right order). Shrinking the LV first destroys data. Always have a backup before any shrink.

Swap: overflow memory

Swap is disk space the kernel uses when RAM is full: it moves memory pages that have not been used for a while out to disk. Swap is far slower than RAM, so it is a safety net, not extra speed. A little swap lets the system survive a memory spike instead of the kernel's out-of-memory killer ending a process.

bash
$ free -h
               total        used        free      shared  buff/cache   available
Mem:           3.6Gi       1.7Gi       512Mi        12Mi       1.4Gi       1.7Gi
Swap:          2.0Gi          0B       2.0Gi
$ swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2

Adding swap space

bash
# lvcreate -n lvswap -L 1G vgdata        # or use a partition of type swap
# mkswap /dev/vgdata/lvswap
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=3f4e5d6c-7b8a-4c9d-8e1f-2a3b4c5d6e7f
# echo "/dev/vgdata/lvswap none swap defaults 0 0" >> /etc/fstab
# swapon -a                     # turn on every swap in fstab
# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2
/dev/dm-3 partition   1G   0B   -3
For a swap partition, use its UUID from mkswap or blkid in fstab: UUID=... none swap defaults 0 0.

How much swap? There is no single rule today. A common starting point: as much as RAM for small machines (under 2 to 4 GB), and a few GB for bigger servers, more if you use hibernation. Databases often prefer little swap. Follow your application's guide.

Common mistakes

  • lvextend -L 1G when the LV is already 2 GiB: that asks to make it 1 GiB TOTAL, which lvextend refuses. Use -L +1G to add.
  • Running xfs_growfs on the device path instead of the mount point on older versions; give it the mount point.
  • Adding a swap line to fstab but never testing with swapon -a. A typo then shows up only at boot.
  • Making swap on a partition with the wrong type, or running mkswap on a partition that holds data.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Growing storage and adding swap · 1/7

no downtime

With LVM you can grow a volume while it is mounted and in use.

Prefer a video by a teacher? Find free videos on “lvextend resizefs vgextend swap mkswap swapon rhel” on YouTube ↗

In real life: Results day at a state exam board

On results day, a state exam board's result portal logs every request, and /var/log is filling fast. Because /var/log is an LV, the admin extends it by 20 GB with lvextend -r in under a minute, while the site keeps serving millions of results. Next week the team adds a new disk with vgextend so there is free space in the group for next year.

bash
# lvextend -r -L +20G /dev/vglogs/lvvarlog
  Size of logical volume vglogs/lvvarlog changed from 30.00 GiB (7680 extents) to 50.00 GiB (12800 extents).
  Logical volume vglogs/lvvarlog successfully resized.
# df -h /var/log
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/vglogs-lvvarlog   50G   29G   22G  57% /var/log

Practice (0/3 done)

Task 1 · Question

Which command adds 500 MiB to /dev/vg01/lv01 AND grows its file system?

Task 2 · Code · Python

A log disk has free_gb free and grows by daily_gb each day. Write days_until_full(free_gb, daily_gb) that returns the number of FULL days before the disk is full (round down). If daily_gb is 0 or less, return None (it never fills). Example: 50 GB free, 6 GB a day → 8 days.

Task 3 · Practice

Grow your /data LV (from the last lesson) by 200 MiB with one command, then add a 512 MiB swap LV that turns on at every boot.

Quick quiz

Question 1

1.Which command grows a mounted XFS file system to fill its LV?

Question 2

2.The VG has no free space. What do you do before lvextend?

Question 3

3.What is swap?

Question 4

4.Which command shows active swap areas?

Question 5

5.Why is shrinking an LV risky?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which command grows a mounted XFS file system to fill its LV?

Answer: xfs_growfs /data

xfs_growfs takes the mount point.

The VG has no free space. What do you do before lvextend?

Answer: Add a PV with pvcreate and vgextend

vgextend adds space to the group.

What is swap?

Answer: Disk space used when RAM is full

It is slow overflow memory on disk.

Which command shows active swap areas?

Answer: swapon --show

swapon --show (or free -h for totals).

Why is shrinking an LV risky?

Answer: If the LV shrinks before the file system, data is destroyed

Shrink the file system first (ext4 only), or use lvreduce -r, and have a backup.

Which command lists volume groups with their free space?Related

Answer: vgs

vgs shows VSize and VFree.

From LVM: physical volumes, volume groups and logical volumes

Which command mounts everything listed in fstab that is not yet mounted?Related

Answer: mount -a

mount -a reads fstab and mounts what is missing.

From Permanent mounts with /etc/fstab and UUIDs

Which command shows file system types and UUIDs of block devices?Related

Answer: lsblk -f

lsblk -f (or blkid) shows FSTYPE and UUID.

From File systems and mounting: XFS, ext4, mount and df

What is the usual default general purpose EBS volume type today?Related

Answer: gp3

gp3 is the general purpose SSD with a baseline of 3,000 IOPS and 125 MiB/s included.

From Block and file storage: EBS, instance store and EFS

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. This is the last topic of “Storage: partitions, file systems and LVM”. The module exam comes next.
  2. Module 5 exam: Storage · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, SELinux and SSH. 57 topics left in the course before the final exam.

← Previous: LVM: physical volumes, volume groups and logical volumes