Sub topic 10
Backups, Shrinking Limits, and Common Mistakes
This chapter assumes you have already followed the earlier steps in this tutorial. You should be comfortable with pvs, vgs, lvs, lsblk and df. You should know how lvextend and the filesystem resize tools work. You should also be able to read the layout of a physical volume (PV), volume group (VG) and logical volume (LV). The examples keep using the volume group vg_data, the logical volume lv_app and the mount point /srv/data.
Growing an LV is one of the safer storage changes you can make. It adds space and does not move existing data. Even so, a wrong device name, a wrong command order or a half-finished resize can cost you a filesystem. This chapter covers three things: how to protect yourself before a change, why shrinking is a different kind of operation from growing, and which mistakes show up most often.
Growing Is Low Risk, Not No Risk
When you extend an LV and then grow its filesystem, the existing blocks stay where they are. LVM adds extents to the end of the LV, and the filesystem then claims the new space. That is why you can do it on a mounted, running server.
Risk still comes from several places:
- A mistyped device name in
pvcreatecan destroy data on a disk that was in use. - A power loss or kernel crash in the middle of a resize can leave metadata in an odd state.
- A change to the partition table (
growpart) acts on the wrong partition number. - Shrinking, which looks like the mirror image of growing, is not symmetrical. A mistake there truncates your data.
- Spanning an LV across several disks makes the volume depend on all of them.
The rest of the chapter addresses these in turn.
Backups Before You Resize
An LV resize is routine, but a routine change on a production data volume still deserves a recovery path. You need two kinds of protection, and they cover different failures.
Back up the LVM metadata
LVM metadata describes which extents belong to which LV. It lives on every PV, and LVM also keeps text copies on the host. Each change writes an archive copy to /etc/lvm/archive/, and the latest state is kept in /etc/lvm/backup/. You can also write an explicit copy to a place you choose:
sudo vgcfgbackup -f /root/vg_data-before-resize.vgcfg vg_data
sudo vgcfgrestore --list vg_dataThe first command saves the layout of vg_data to a file. The second lists the archived versions that LVM can restore.
A metadata backup does not contain your files. It lets you put the layout back, for example after you remove the wrong LV. It does not bring back data that was overwritten.
Back up the data
For the data itself, use your normal backup tool, such as rsync, restic, borg, or a database dump. The test of a backup is whether you can restore from it. Before a risky change, confirm that the most recent backup exists and that you have restored at least a few files from it.
Snapshots are a short-term safety net
LVM can create a snapshot, a point-in-time view of an LV. It is handy for taking a consistent backup of a busy volume, or for a quick rollback point during a change.
sudo lvcreate --snapshot -L 5G -n lv_app_snap /dev/vg_data/lv_app
sudo mkdir -p /mnt/snap
sudo mount -o ro /dev/vg_data/lv_app_snap /mnt/snap
# ... copy files from /mnt/snap with your backup tool ...
sudo umount /mnt/snap
sudo lvremove vg_data/lv_app_snapThe -L 5G is not the size of the data. It is the space reserved to hold blocks that change on the origin LV after the snapshot is taken. Two points matter here:
- If the origin changes more than 5 GiB while the snapshot exists, the snapshot fills up and becomes invalid. Check
lvsand watch theData%column. - A snapshot lives in the same volume group, usually on the same disks. If a disk dies, you lose the origin and the snapshot together.
For XFS, mounting a snapshot while the origin is mounted fails, because both filesystems carry the same UUID. Add the nouuid option: sudo mount -o ro,nouuid /dev/vg_data/lv_app_snap /mnt/snap.
A pre-flight checklist
Run through this list before any resize on a server that matters:
- Run
lsblk -fandsudo pvsand read the output to confirm the device you are about to touch is the one you intend. - Confirm that a recent data backup exists and has been tested.
- Save the metadata with
vgcfgbackup. - Check
df -hTanddf -iso you know the filesystem type and whether inodes are a concern. - Check
dmesgorjournalctl -kfor I/O errors on the disks involved. - Have a console, not only an SSH session, if the volume holds the root filesystem.
Why Shrinking Is Different
Growing and shrinking are not two directions of one operation. When you grow, the filesystem gets new, empty space. When you shrink, the filesystem has to relocate every block that sits in the part you are removing, and only then can the LV be cut.
The consequences depend on the filesystem:
| Filesystem | Grow while mounted | Shrink |
|---|---|---|
| ext4 | Yes | Only unmounted, after a filesystem check |
| XFS | Yes | Not supported at all |
XFS has no shrink operation. If you made an XFS volume too large, the only path is to create a smaller LV, copy the data into it, and switch over. ext4 can shrink, but it must be unmounted first, and the order of the steps is the opposite of growing.
The rule to remember is that the filesystem must always fit inside the LV. When growing, you enlarge the container first and the contents second. When shrinking, you reduce the contents first and the container second. If you reverse the shrink order, you cut off the end of the filesystem while it still holds data.
Shrinking an ext4 volume
This example reduces lv_app to 40 GiB. It assumes the data currently in the filesystem uses well under 40 GiB. Confirm that with df first, and take a backup.
sudo umount /srv/data
sudo e2fsck -f /dev/vg_data/lv_app
sudo resize2fs /dev/vg_data/lv_app 40G
sudo lvreduce -L 40G /dev/vg_data/lv_app
sudo mount /dev/vg_data/lv_app /srv/data
df -hT /srv/datae2fsck -f forces a check, and resize2fs refuses to shrink a filesystem that has not been checked recently. resize2fs shrinks the filesystem to 40 GiB, and lvreduce then trims the LV to the same size.
You can let LVM run these steps in the right order with the -r option:
sudo lvreduce -r -L 40G /dev/vg_data/lv_appWith -r, LVM unmounts the filesystem, checks it, shrinks it, reduces the LV and remounts it. It also asks for confirmation before reducing, which you should read, not skip.
Making an XFS volume smaller
Because XFS cannot shrink, you replace the volume. The outline looks like this:
sudo lvcreate -L 40G -n lv_app_new vg_data
sudo mkfs.xfs /dev/vg_data/lv_app_new
sudo mkdir -p /mnt/new
sudo mount /dev/vg_data/lv_app_new /mnt/new
sudo rsync -aHAXx /srv/data/ /mnt/new/Run the copy once while the service is running to move the bulk of the data. Then stop the service, run the rsync again to catch the last changes, unmount both volumes, mount the new one at /srv/data, update /etc/fstab, and start the service. Remove the old LV only after you have confirmed that the new one works. If you prefer, xfsdump piped into xfsrestore does the same job and preserves XFS-specific attributes.
Removing a disk from a volume group
Taking a PV out of service is also a shrink of the volume group. You first move its extents elsewhere, so the other PVs need enough free space to receive them:
sudo pvmove /dev/sdb
sudo vgreduce vg_data /dev/sdb
sudo pvremove /dev/sdbpvmove copies the extents off /dev/sdb while the volumes stay online. It can take a long time on a large disk, and it is safest to run it inside tmux or screen. If it is interrupted, running pvmove with no arguments resumes or cleans up the operation. vgreduce removes the now-empty PV from the group, and pvremove wipes the LVM label.
Common Mistakes
The following mistakes are frequent and each has a specific cause.
| Mistake | What happens | How to avoid it |
|---|---|---|
| Extending the LV but not the filesystem | `lvs` shows the new size, `df` does not | Run `resize2fs` or `xfs_growfs`, or use `lvextend -r` |
| Running `xfs_growfs` on an unmounted volume | The command fails, since XFS must be mounted | Mount the filesystem, then pass the mount point |
| Using `-L 20G` when you meant `-L +20G` | Sets the total size to 20 GiB, not an increase of 20 GiB | Always put the plus sign on increases |
| Using `lvresize` with an absolute size | Can shrink the LV if the number is smaller than the current size | Use `lvextend`, which refuses to reduce |
| `pvcreate` on the wrong device | Overwrites the LVM label on a disk that was in use | Check `lsblk -f` and `pvs` first |
| `growpart` on the wrong partition number | Edits a partition you did not mean to change | Check `lsblk` for the partition that holds the PV |
| Forgetting `pvresize` after enlarging a disk | The VG shows no new free space | Run `pvresize` on the partition after `growpart` |
| Giving all free space to one LV | No room left for snapshots or other LVs | Leave some free space with `-l +80%FREE` or a fixed size |
The plus sign
The difference between -L 20G and -L +20G deserves attention. The first says "make the LV 20 GiB in total". The second says "add 20 GiB". On a 100 GiB LV, lvextend -L 20G refuses because the target is smaller than the current size. lvresize -L 20G on the same LV would try to shrink it to 20 GiB. Using lvextend for growing gives you a safety check that lvresize does not provide.
Growing too far, too fast
An XFS filesystem divides itself into allocation groups when it is created, and the number depends on the size at that time. If you create a small XFS filesystem and later grow it by a large factor, such as from 10 GiB to several terabytes, it keeps a very large number of small groups. This can hurt performance and make later changes awkward. For volumes you expect to grow a lot, create them near the size you expect to need, or plan to migrate to a new volume once growth passes about an order of magnitude.
Spanning disks without redundancy
When you add a disk with vgextend and extend a linear LV onto it, the LV now depends on both disks. If either disk fails, the LV is damaged, and the chance of a failure goes up with every disk you add. LVM does not add redundancy by itself. Put the PVs on hardware RAID or software RAID (mdadm), or use LVM's own RAID LV types, and keep tested backups. Check this before you extend onto a single bare disk for something important.
Inodes and reserved space
A volume can report free space and still fail to create files. On ext4 the inode count is fixed when the filesystem is made, and growing the filesystem adds inodes in proportion, but a volume that ran out of inodes needs the growth to actually happen. Check df -i. ext4 also reserves 5% of blocks for root by default, so df can show less available space than you expect. You can inspect this with sudo tune2fs -l /dev/vg_data/lv_app and adjust it with tune2fs -m.
Boot and root volumes
If the LV holds / or /var, growing it online still works, but you do not have the option to unmount it, so ext4 shrinking is impossible without booting from rescue media. The /boot partition is usually outside LVM, and it is not enlarged by anything in this chapter.
When Something Goes Wrong
If you reduced an LV too far or removed the wrong volume, stop writing to the disks at once. Further writes can overwrite the data that is still recoverable.
- List the archived metadata with
sudo vgcfgrestore --list vg_data. Each entry shows the command that produced it and a timestamp. - Pick the version from just before the mistake.
- Restore it with
sudo vgcfgrestore -f /etc/lvm/archive/<file> vg_data. The VG's LVs must be inactive for this to work. - Activate the volumes with
sudo vgchange -ay vg_dataand run a read-only filesystem check, for examplesudo e2fsck -n /dev/vg_data/lv_app.
This restores the layout. If the data in the removed area was never overwritten, a filesystem check may find it intact. If a shrunk filesystem was truncated, some files are gone and only your backup brings them back. Keep any such rescue work on a copy of the disk if the data is valuable.
Summary of Rules
- Back up the data and the LVM metadata before you change a volume.
- Treat snapshots as temporary, not as backups.
- Grow in the order LV first, filesystem second. Shrink in the order filesystem first, LV second.
- XFS can grow but never shrink. ext4 can shrink only while unmounted.
- Use
lvextendfor growth and+for increments. - Check device names with
lsblk -fandpvsbefore any command that writes labels or partitions. - Remember that more disks in a linear LV means more ways to lose it.
Exercise
A 100 GiB ext4 LV named lv_app is mounted at /srv/data and holds 30 GiB of data. You want to reduce it to 50 GiB. List the commands in the correct order, and explain what would go wrong if you ran lvreduce first.
Show answer
Unmount with sudo umount /srv/data, check with sudo e2fsck -f /dev/vg_data/lv_app, shrink the filesystem with sudo resize2fs /dev/vg_data/lv_app 50G, reduce the LV with sudo lvreduce -L 50G /dev/vg_data/lv_app, then mount again and check with df -hT /srv/data. Alternatively, sudo lvreduce -r -L 50G /dev/vg_data/lv_app does the steps in the right order. If you ran lvreduce first, the LV would be cut to 50 GiB while the filesystem still believed it was 100 GiB. Blocks in the removed half, which may hold file data or metadata, would be lost, and the filesystem would be corrupt.
Exercise
Your XFS volume /srv/data is 500 GiB but needs only 100 GiB, and you want the rest back for other LVs. Why can't you use lvreduce -r, and what do you do instead?
Show answer
XFS does not support shrinking, so there is no tool that can make the filesystem smaller and the LV reduction would destroy data. Create a new smaller LV (for example 100 GiB), make an XFS filesystem on it, copy the data over with rsync or xfsdump and xfsrestore (a first pass while the service runs, a final pass after stopping it), mount the new volume at /srv/data, update /etc/fstab, verify, and then remove the old LV with lvremove so its extents return to the volume group.
Exercise
A colleague runs sudo lvresize -L 80G /dev/vg_data/lv_app on an LV that is currently 120 GiB, meaning to add 80 GiB. What does the command do, and which command and option would have been safer?
Show answer
Without a plus sign, -L 80G sets the total size to 80 GiB. Because the LV is 120 GiB, lvresize tries to shrink it, which can truncate the filesystem. The safer command is sudo lvextend -r -L +80G /dev/vg_data/lv_app. It only grows, refuses a target smaller than the current size, and -r grows the filesystem in the same step.