How to Extend a Logical Volume in LVM - for XFS, ext4 and swap filesystem
Extending a logical volume and XFS file system
One benefit of logical volumes is the ability to increase their size without experiencing downtime. Free physical extents in a volume group can be added to a logical volume to extend its capacity, which can then be used to extend the file system it contains.
Extending a Logical Volume
To extend a logical volume, perform the following steps:
1. Verify that the volume group has space available.
Usevgdisplay to verify that there are sufficient physical extents available:
[root@host ~]# vgdisplay vg01
--- Volume group --
VG Name vg01
...output omitted...
Free PE / Size 178 / 712.00 MiB
...output omitted...
Inspect the Free PE / Size in the output. Confirm that the volume group has sufficient free space for the LV extension. If insufficient space is available, then extend the volume group appropriately.
2. Extend the logical volume
Use lvextend LV_DEVICE_NAME to extend the logical volume to a new size.
[root@host ~]# lvextend -L +300M /dev/vg01/lv01
This increases the size of the logical volume lv01 by 300 MiB. Notice the plus sign (+) in front of the size, which means add this value to the existing size; otherwise, the value defines the final size of the LV.
As with lvcreate, different methods exist to specify the size: the -l option expects the number of physical extents as the argument. The -L option expects sizes in bytes, mebibytes, gibibytes, and similar.
The following list provides some examples of extending LVs.
COMMAND | RESULTS |
---|---|
lvextend -l 128 | Resize the logical volume to exactly 128 extents in size. |
lvextend -l +128 | Add 128 extents to the current size of the logical volume. |
lvextend -L 128M | Resize the logical volume to exactly 128 MiB. |
lvextend -L +128M | Add 128 MiB to the current size of the logical volume. |
3. Extend the file system
Use xfs_growfs mountpoint to expand the file system to occupy the extended LV. The target file system must be mounted when you use the xfs_growfs command. You can continue to use the file system while it is being resized.
[root@host ~]# xfs_growfs /mnt/data
4. Verify
Verify the new size of the mounted file system:
# df -h /mountpoint
Extending a logical volume and EXT4 file system
The steps for extending an ext4-based logical volume are essentially the same as for an XFSbased LV, except for the step that resizes the file system. Review the section called “Extending a Logical Volume and XFS File System”.
1. Verify that the volume group has space available
Use vgdisplay VGNAME to verify that the volume group has a sufficient number of physical extents available.
[root@host ~]# vgdisplay vg01
--- Volume group --
VG Name vg01
...output omitted...
Free PE / Size 178 / 712.00 MiB
...output omitted...
Inspect the Free PE / Size in the output. Confirm that the volume group has sufficient free space for the LV extension. If insufficient space is available, then extend the volume group appropriately.
2. Extend the logical volume
Use lvextend -l +extents /dev/vgname/lvname to extend the logical volume /dev/ vgname/lvname by the extents value.
3. Extend the file system
Use resize2fs /dev/vgname/lvname to expand the file system to occupy the new extended LV. The file system can be mounted and in use while the extension command is running. You can include the -p option to monitor the progress of the resize operation.
[root@host ~]# resize2fs /dev/vg01/lv01
Extend a logical volume and swap space
Logical volumes formatted as swap space can be extended as well, however the process is different than the one for extending a file system, such as ext4 or XFS. Logical volumes formatted with a file system can be extended dynamically with no downtime. Logical volumes formatted with swap space must be taken offline in order to extend them.
1. Verify the volume group has space available
Use vgdisplay vgname to verify that a sufficient number of free physical extents are available.
[root@host ~]# vgdisplay vg01
--- Volume group --
VG Name vg01
...output omitted...
Free PE / Size 178 / 712.00 MiB
...output omitted...
Inspect the Free PE / Size in the output. Confirm that the volume group has sufficient free space for the LV extension. If insufficient space is available, then extend the volume group appropriately.
2. Deactivate the swap space
Use swapoff -v /dev/vgname/lvname to deactivate the swap space on the logical volume.
# swapoff -v [/dev/vgname/lvname]
WARNING: Your system must have enough free memory or swap space to accept anything that needs to page in when the swap space on the logical volume is deactivated
3. Extend the logical volume
lvextend -l +extents /dev/vgname/lvname extends the logical volume /dev/vgname/ lvname by the extents value.
# lvextend -l +extents [/dev/vgname/lvname]
4. Format the logical volume as swap space
mkswap /dev/vgname/lvname formats the entire logical volume as swap space.
# mkswap [/dev/vgname/lvname]
5. Activate the swap space
Use swapon -va /dev/vgname/lvname to activate the swap space on the logical volume.
# swapon -va [/dev/vgname/lvname]