I have 4 LVs, type is ext4. I want to know how to safely extend them since they are all used for production.
From what I know the steps are.
- unmount
- extend
- resize FS
- mount again
Are these steps correct or is there something I am missing?
The answer depends on what we're calling a logical volume. Assuming we're talking about LVM style setups, there are three components to an LVM setup:
Physical volumes (PVs) - partitions on drives that're formatted as LVM2 members.
Volume groups (VGs) - groupings of volumes on different PVs.
Logical volumes (LVs) - logical partitions/volumes which are created as part of VGs.
ALWAYS make sure you back up mission critical data before fussing with your system schemas and partitions. Even in LVM environments!
If you are intending to add new PVs to volume groups you can do this relatively safely without any special actions. If you are going to shrink or extend existing PVs, you should do so extremely carefully and probably from an unmounted state.
If you are going to be adding new VGs or adding PVs to VGs, you can do this without much extra effort. If you're removing or shrinking VGs because of changing PVs, then do so from a safer state where you're not running on the VGs / LVs itself.
Specifically with Logical Volumes that're ext4
filesystems, we can relatively easily extend them without issues. And we can do it live without unmounting the filesystem.
There are a few prerequisites we have to make sure though:
The PVs that the LVs and VGs are part of have been properly expanded to accommodate new free space (so the VGs and LVs can extend into the extents).
The VGs reflect the larger 'free space' and it shows as unused when you look at vgs
output.
The LV is part of a VG that has the extra space / extents.
If all these prerequisites are met, then you can do a live resize/extend and extend the LV and its corresponding filesystem.
Double check that pvs
and vgs
reflect the changes in disk and that there are free extents on the VG.
Extend the LV to take up the space you want. In this example, I'm extending the LV to fill all 100% available 'free' extents in the VG (which is quite common when dealing with expanding file storage needs). In this example, the VG in vgs
is vg-ubuntu22
and the LV is named root
for my root partition. It shows in /dev/mapper/
accordingly as vg--ubuntu22-root
.
sudo lvextend -l +100%FREE -r /dev/mapper/vg--ubuntu22-root
Extend the filesystem. Since this is an ext4
filesystem, we can safely extend the volume without any need to unmount, etc.
sudo resize2fs /dev/mapper/vg--ubuntu22-root
The system will now reflect the larger filesystem size after doing a live resize. This does NOT work on all filesystems, but works extremely well on ext4, and ONLY works on extending. It is not safe to shrink a filesystem while it's running.
lvresize -l +100%FREE -r /dev/mapper/vg--ubuntu22-root
will extend the logical volume to take up the entire free space in the VG and resize the file system to match the LV size. This can be done live. No need to run resize2fs
or the equivalent for xfs/zfs/blah.
-r
because I'm old school and don't trust tools to properly chain together for that component ;)
lvresize -l +100%FREE -r /dev/mapper/blah
but you are really missing many details. And if you're really running 12.04, the post should be deleted since this is a long-dead version and since you haven't upgraded to a supported version in many years.-r
flag tolvresize
saves one step in your answer. You can automagically resize the file system to match the logical volume size with this. It will not work for shrinking most file systems but will work for expanding logical volumes.