Resize an LVM file system

From ezUnix
Jump to: navigation, search
                                    pdf_icon.png Download this article as a single PDF document 

Introduction

In this HowTo I will show you how to resize an existing ext3 file system and it's associate LV.
Logical Volumes can be increased or decreased in size depending on your needs.
However, resizing the LV does not eliminate the need to resize the file system contained within the LV.
This is an important concept to understand since resizing the LV without resizing the file system can cause corruption of your data.

NOTE: Resizing a file system can also be destructive if not done properly. You should always make sure you have a backup of your data before attempting a resize!


Grow a file system

Increasing the size of a file system managed with LVM can be done online (with the file system mounted.)
In order to grow the LV and file system:

  • Check to see if free space exists on the LV that contains the file system
  • Expand the LV if it does not contain enough free space (which could require expanding the volume group if it is out of free space).
  • Grow the file system to utilize all available space on the LV


First, check the size of the file system to see if it needs expanding:

#df -h 
/dev/mapper/localvg-home
                      15G  4.2G  9.7G  31% /home


Now let's find out which Volume Group contains the Logical Volume that holds /dev/mapper/localvg-home

# lvdisplay /dev/localvg/home
 --- Logical volume ---
 LV Name                /dev/localvg/home
 VG Name                localvg
 LV UUID                iK3AmV-cwEY-7Yg8-3VUD-lCX7-tY3J-nFy3YT
 LV Write Access        read/write
 LV Status              available
 # open                 1
 LV Size                15.00 GB
 Current LE             480
 Segments               2
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:1
  

The value of VG Name tells us it's localvg.


Let's find out if the volume group localvg has available free space to allocate to the logical volume:

# vgdisplay localvg
 --- Volume group ---
 VG Name               localvg
 System ID             
 Format                lvm2
 Metadata Areas        1
 Metadata Sequence No  9
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                7
 Open LV               7
 Max PV                0
 Cur PV                1
 Act PV                1
 VG Size               67.59 GB
 PE Size               32.00 MB
 Total PE              2163
 Alloc PE / Size       1696 / 53.00 GB
 Free  PE / Size       467 / 14.59 GB
 VG UUID               4iHPOY-eZOn-jPPN-PX5E-1I35-2LPu-HErSn8

Here the value of Free PE says we have 14.59GB left.


We will now resize the logical volume localvg by adding 3GB

# lvresize -L +3GB /dev/localvg/home

NOTE: To use all the remaining space you can just run: lvresize -l +100%FREE


Confirm the new size of the logical volume

# lvdisplay /dev/localvg/home 
  --- Logical volume ---
  LV Name                /dev/localvg/home
  VG Name                localvg
  LV UUID                iK3AmV-cwEY-7Yg8-3VUD-lCX7-tY3J-nFy3YT
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                18.00 GB
  Current LE             480
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

You will notice that although we have increased the size of the logical volume, the size of the file system has been unaffected.
We now need to resize the ext3 file system to utilize the remaining available space within the logical volume:

# resize2fs -p /dev/mapper/localvg-home 

You just resized your file system while it was online!
Now lets take a look at reducing the size of a file system.


Shrink a file system

Decreasing the size of a file system managed with LVM must be done off-line (unmounted.)
To shrink the file system and LV you need to:

  • Unmount the file system
  • Run a file system check to ensure the integrity of the volume
  • Shrink the file system
  • Shrink the logical volume


NOTE: You cannot shrink the file system beyond the amount of free space that is available on it.
So if the file system you want to shrink has 1GB of free space, you will only be able to shrink the volume by 1GB.
However, logical volumes are not as forgiving. If you are not careful, you can shrink the LV to a size less than what is required by the file system.
If the LV is resized smaller than what the file system has been resized to, things will go very badly for you.
Did we mention you should backup your data before hand? ;)


First, check to see how much space is available

# df -h
Filesystem	             Size	Used	Avail	Use%	Mounted on
/dev/mapper/localvg-home 6.1G	922M	4.9G	16%	/home

NOTE: The disk free command shows that we are using 922MB and have 4.9G available on our file system.
Therefore, we can safely shrink the volume to 1.5G (leaving a little bit for overhead) without any issue.


Unmount the file system

# umount /home

Check the file system for errors:

# e2fsck -f /dev/mapper/localvg-home

Shrink the file system to 1.5GB:

# resize2fs /dev/mapper/localvg-home 1500M

Shrink the logical file system to 1.5GB

# lvresize -L 1.5G /dev/localvg/home

NOTE: Special precaution should be taken with this step.
It's possible to reduce the logical volume size by more than the size of the file system.
If you do reduce the LV size by more than what you resized the file system to (from step #4), this will almost certainly end very badly for you.
Ensure the LV is large enough for the file system and that you make a backup before hand!

Verify the new size of the logical volume:

# lvdisplay /dev/localvg/home 

Remount the file system and verify the new size

# mount /home
# df -h



That's all folks.
Marcin


<comments />