Intermediate · Storage: partitions, file systems and LVM · topic 28 of 57 · 35 min
Growing storage and adding swap
Explanation
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.
# 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| Command | Meaning |
|---|---|
| lvextend -L +1G | Add 1 GiB to the current size |
| lvextend -L 5G | Make the total size 5 GiB |
| lvextend -l +50 | Add 50 extents |
| lvextend -l +100%FREE | Add 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
# 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/lvdataSwap: 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.
$ 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 -2Adding swap space
# 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 -3How 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
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.
# 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/logPractice (0/3 done)
Which command adds 500 MiB to /dev/vg01/lv01 AND grows its file system?
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
- File systems and mounting: XFS, ext4, mount and dfStorage: partitions, file systems and LVM
- Permanent mounts with /etc/fstab and UUIDsStorage: partitions, file systems and LVM
- LVM: physical volumes, volume groups and logical volumesStorage: partitions, file systems and LVM · the topic just before this one
Related topics
- Block and file storage: EBS, instance store and EFSCloud Computing (AWS, Azure, Google Cloud) · Compute and storage: EC2 and S3
- Running containers: ports, volumes, networks and limitsDevOps: Git, Docker, Kubernetes and CI/CD · Docker
- Pointers and functionsC Programming (from zero to systems) · Pointers
- Space complexity and amortised costData Structures and Algorithms (DSA) for interviews · Thinking in Big-O
- Docker Compose: multi-container appsDevOps: Git, Docker, Kubernetes and CI/CD · Docker
- Stack and heapC Programming (from zero to systems) · Dynamic memory
Questions people ask about this
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
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.
Which command shows file system types and UUIDs of block devices?Related
Answer: lsblk -f
lsblk -f (or blkid) shows FSTYPE and UUID.
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.
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
- This is the last topic of “Storage: partitions, file systems and LVM”. The module exam comes next.
- Module 5 exam: Storage · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- 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