Resizing a logical volume under Linux is a two step process; first the logical volume itself must be resized, and then the filesystem itself resized.
Contents
Logical Volume resize
lvextend
is the command to resize the logical volume. The -L
option specifies the number of megabytes to resize to, or +
will indicate this is the number of megabytes to add to the current size. Other units can be appended to the size to indicated according to the following table:
- M – megabytes
- G – gigabytes
- T – Terabytes
- P – Petabytes
- E – Exabytes
The LV to be resized is indicated in the format volumegroupname/logicalvolumename. This can be checked by observing the output of df
; for example, /dev/mapper/rootvg-varlv would give a name of rootvg/varlv to be resized.
A sample command to add 2 gigabytes to /dev/mapper/rootvg-varlv mounted on /var would be:
lvextend -L +2G rootvg/varlv
Filesystem resize
Unlike AIX, resizing the filesystem is a separate step. Under Debian, the resize2fs
command is used. The filesystem device itself (/dev/mapper/rootvg-varlv
in the previous example) is used. For example:
resize2fs /dev/mapper/rootvg-varlv
This will re-size the partition up to the maximum number of blocks available.
Older systems (eg, older versions of Redhat) use the ext2online
command (which will work on ext3 partitions as well, despite its name). ext2online works against the device node as well, so a sample command would be the same:
ext2online /dev/mapper/rootvg-varlv
Gotcha:
In the case of reducing, the steps have to be reversed, otherwise you will cause a total corruption of the LV (which believe me, isn’t fun). So to reduce one would reduce the File System first, and then reduce the Logical Volume. Note: The file system has to be un-mounted for a reduce operation.
example:
For this example, I’m going to use the home partition, as that is typically where most people add their space to. For this operation to work, you have to either:
1) reboot to single user mode
2) jump out to a tty terminal and bring X-windows down (gracefully preferably)
3) boot a live media, and modify the Vg/LVs that way
I am using Debian, so I can login as root without a problem. In the case of Ubuntu, you may wish to use option 1, or to temporarily enable the root user the ability to login and do the work that way.
I’ll assume you have already connected to a virtual terminal and shut-down X-windows. Here is the current layout:
VGname = rootvg (size = 250GB ) — Currently rootvg has 0 bytes available, as it’s all been allocated.
LVs = homelv (200GB), rootlv (300M), varlv (2,5GB), tmplv (500MB), etc….
Note: All LVs are ext4, except /boot, which is ext2.
umount /home #unmount File System
e2fsck -f /dev/rootvg/home # do check of filesystem
resize2fs /dev/rootvg/home 150G # Make new size of LV=150GB (thus reduce by 50GB)
lvreduce -L -50G rootvg/home
Now that you have the additional space added back to your VG, you can increase other file systems, create LVs, etc. If you don’t need to do any of that, simply reboot or load X-windows back up.
NOTE: Before attempting any LV reductions, a proper backup should be made!
Nice write I did one on LVM few weeks back, when I had re re-size my mail server data folder
Well I definitely enjoyed reading it. This subject offered by you is very useful for accurate planning.
Hello.This post was really fascinating, particularly because I was browsing for thoughts on this subject last week.
Thanks for all of the kind words folks. Hope it was useful.