Tuesday, September 13, 2011

LVM Resizing Guide


(19 votes, average 4.95 out of 5)
Written by Tom Hirt   
Friday, 15 May 2009 09:02
Article Index
LVM Resizing Guide
Grow File System
Shrink File System
All Pages

LVM Resizing Overview


In our LVM Configuration KB, we showed how to create a linear mapped logical volume (LV).  In this KB, we are going to build on our LVM knowledge and show you how to resize an existing ext3 file system and it's associate LV.
Note: Procedures for resizing XFS, ResierFS and other volumes may differ.  You should check your documentation for non ext3 volumes before proceeding.  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!
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.
In the following pages, we will guide you through expanding and contracing LV's and their associated file systems.  Let's get to it!



Grow 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
Lets get started!
    1. First, check the size of the file system to see if it needs expanding

      [root@Linux01 ~]# pwd
      /TCPDumpLV
      [root@Linux01 TCPDumpLV]# df -kh .
      FilesystemSizeUsedAvailUse%Mounted on
      /dev/mapper/TCPDumpVolGRP-TCPDumpLV3.1G2.9G69M98%/TCPDumpLV
      Note: The disk free command shows that we have 65MB available on our file system and that its 98% use.  If we don't take action soon, we risk filling the file system.
    2. Let's find out which Volume Group contains the Logical Volume that holds /dev/mapper/TCPDumpVolGRP-TCPDumpLV

      [root@Linux01 ~]# lvdisplay /dev/TCPDumpVolGRP/TCPDumpLV
      --- Logical volume ---
      LV Name/dev/TCPDumpVolGRP/TCPDumpLV
      VG NameTCPDumpVolGRP
      LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
      LV Write Accessread/write
      LV Statusavailable
      # open1
      LV Size3.12 GB
      Current LE100
      Segments1
      Allocationinherit
      Read ahead sectorsauto
      - currently set to256
      Block device253:5
      Note: You can see the volume group for this file system is TCPDumpVolGRP
    3. Let's find out if the volume group TCPDumpVolGRP has available free space to allocate to the logical volume

      [root@Linux01 ~]# vgdisplay TCPDumpVolGRP
      --- Volume group ---
      VG NameTCPDumpVolGRP
      System ID
      Formatlvm2
      Metadata Areas3
      Metadata Sequence No5
      VG Accessread/write
      VG Statusresizable
      MAX LV0
      Cur LV2
      Open LV2
      Max PV0
      Cur PV3
      Act PV3
      VG Size11.91 GB
      PE Size32.00 MB
      Total PE381
      Alloc PE / Size228 / 7.12 GB
      Free PE / Size153 / 4.78 GB
      VG UUID9fWFIS-vDlg-xOW6-Xmb8-Tkrg-GPZw-ZnUZwh
      Note: This volume group has plenty of free space.  If we were out of physical extents, we would have to add additional physical volumes to this volume group before continuing on.
    4. We will now resize the logical volume TCPDumpLV by adding 3GB

      [root@Linux01 TCPDumpLV]# lvresize -L +3GB /dev/TCPDumpVolGRP/TCPDumpLV
      Extending logical volume TCPDumpLV to 6.12 GB
      Logical volume TCPDumpLV successfully resized
      [root@Linux01 TCPDumpLV]#
    5. Confirm the new size of the logical volume

      [root@Linux01 ~]# lvdisplay /dev/TCPDumpVolGRP/TCPDumpLV
      --- Logical volume ---
      LV Name/dev/TCPDumpVolGRP/TCPDumpLV
      VG NameTCPDumpVolGRP
      LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
      LV Write Accessread/write
      LV Statusavailable
      # open1
      LV Size6.12 GB
      Current LE196
      Segments2
      Allocationinherit
      Read ahead sectorsauto
      - currently set to256
      Block device253:5

      [root@Linux01 ~]# pwd
      /TCPDumpLV
      [root@Linux01 TCPDumpLV]# df -kh .
      FilesystemSizeUsedAvailUse%Mounted on
      /dev/mapper/TCPDumpVolGRP-TCPDumpLV3.1G2.9G69M98%/TCPDumpLV
      Note: You will notice that although we have increased the size of the logical volume, the size of the file system has been unaffected.
    6. We now need to resize the ext3 file system to utilize the remaining available space within the logical volume

      [root@Linux01 TCPDumpLV]# resize2fs -p /dev/mapper/TCPDumpVolGRP-TCPDumpLV
      resize2fs 1.39 (29-May-2006)
      Filesystem at /dev/mapper/TCPDumpVolGRP-TCPDumpLV is mounted on /TCPDumpLV; on-line resizing required
      Performing an on-line resize of /dev/mapper/TCPDumpVolGRP-TCPDumpLV to 1605632 (4k) blocks.
      The filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV is now 1605632 blocks long.

      [root@Linux01 TCPDumpLV]# df -kh .
      FilesystemSizeUsedAvailUse%Mounted on
      /dev/mapper/TCPDumpVolGRP-TCPDumpLV6.1G2.9G2.9G50%/TCPDumpLV
    Nice work, you just resized your file system while it was online!  Now lets take a look at reducing the size of a file system.



    Shrink 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:
    • 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?
    Let's get started!
    1. First, check to see how much space is available

      [root@Linux01 ~]# pwd
      /TCPDumpLV
      [root@Linux01 TCPDumpLV]# df -kh .
      FilesystemSizeUsedAvailUse%Mounted on
      /dev/mapper/TCPDumpVolGRP-TCPDumpLV6.1G922M4.9G16%/TCPDumpLV
      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.
    2. Unmount the file system

      [root@Linux01 TCPDumpLV]# cd /
      [root@Linux01 /]# umount /TCPDumpLV
    3. Check the file system for errors

      [root@Linux01 /]# e2fsck -f /dev/mapper/TCPDumpVolGRP-TCPDumpLV
      e2fsck 1.39 (29-May-2006)
      Pass 1: Checking inodes, blocks, and sizes
      Pass 2: Checking directory structure
      Pass 3: Checking directory connectivity
      Pass 4: Checking reference counts
      Pass 5: Checking group summary information
      /dev/mapper/TCPDumpVolGRP-TCPDumpLV: 13/802816 files (7.7% non-contiguous), 261017/1605632 blocks
      [root@Linux01 /]#
    4. Shrink the file system to 1.5GB

      [root@Linux01 /]# resize2fs /dev/mapper/TCPDumpVolGRP-TCPDumpLV 1500M
      resize2fs 1.39 (29-May-2006)
      Resizing the filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV to 384000 (4k) blocks.
      The filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV is now 384000 blocks long.

      [root@Linux01 /]#
    5. Shrink the logical file system to 1.5GB

      [root@Linux01 /]# lvresize -L 1.5G /dev/TCPDumpVolGRP/TCPDumpLV
      WARNING: Reducing active logical volume to 1.50 GB
      THIS MAY DESTROY YOUR DATA (filesystem etc.)
      Do you really want to reduce TCPDumpLV? [y/n]: y
      Reducing logical volume TCPDumpLV to 1.50 GB
      Logical volume TCPDumpLV successfully resized
      [root@Linux01 /]#
      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!
    6. Verify the new size of the logical volume

      [root@Linux01 ~]# lvdisplay /dev/TCPDumpVolGRP/TCPDumpLV
      --- Logical volume ---
      LV Name/dev/TCPDumpVolGRP/TCPDumpLV
      VG NameTCPDumpVolGRP
      LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
      LV Write Accessread/write
      LV Statusavailable
      # open0
      LV Size1.50 GB
      Current LE48
      Segments1
      Allocationinherit
      Read ahead sectorsauto
      - currently set to256
      Block device253:5
    7. Remount the file system and verify the new size

      [root@Linux01 /]# mount /dev/TCPDumpVolGRP/TCPDumpLV
      [root@Linux01 /]# cd /TCPDumpLV/
      [root@Linux01 TCPDumpLV]# df -kh .
      FilesystemSizeUsedAvailUse%Mounted on
      /dev/mapper/TCPDumpVolGRP-TCPDumpLV1.5G920M497M65%/TCPDumpLV

    Nice work, you've just resized your LV and it's file systems!  Next up, LVM snapshots!

    No comments:

    Post a Comment