Reducing Filesystem FS Size and LVM Size in Linux

Quick tutorial of something I did today: reducing filesystem size and LVM size in Linux. My example is based on a Redhat 6.4 box (and thus should work on Centos 6.4). While not verified on other Linuxes, most LVM steps and resize FS steps are the same across linuxes.

Enjoy!

I LIKE TO CRUSH THINGS - Crush WWF

Back Story:

I had to create a test Logical Volume (LV) to test how to encrypt a device, but on my test system there was no more space on my VG. So I had to reduce some space off of a current LVs so I could create a new one

Outline:


Sponsored Links

In this tutorial:

  1. Find a filesystem that is not fully utilized
  2. Reduce the FS by some number of gigabytes
  3. Reduce the LV by some number of gigabytes
  4. Now I have space to create my 1G test LUN

IMPORTANT NOTE:

If you are reducing an LV, you need to make sure to reduce the FS first. And to be on the safe side, reduce the FS by more than you reduce the LV. This is to help keep your data intact.


Sponsored Links

In my example, I will reduce the FS by about 11G, then reduce the LV by about 10G.

THE STEPS: Short Version (for the impatient)

  1. # df (find filesystem and associated LV with space free)
  2. # umount /home (unmount filesystem to shrink it)
  3. # e2fsck -f /dev/vg_redhat64/lv_home (e2fsck before you resize)
  4. # resize2fs -p /dev/vg_redhat64/lv_home 49G (resize to 49G, about 11G less)
  5. # lvreduce -L -10G /dev/vg_redhat/lv_home (reduce LV size by 10G less)
  6. DONE!

Sponsored Links

THE STEPS: Long Version

  1. Find the filesystem to reduce if you do not have one yet. Use df -h to find my LV to reduce

    [root@redhat64 swapfiles]# df
    Filesystem 1K-blocks Used Available Use% Mounted on
    /dev/mapper/vg_redhat64-lv_root
    51606140 1850824 47133876 4% /
    tmpfs 26686388 0 26686388 0% /dev/shm
    /dev/sda2 495844 37360 432884 8% /boot
    /dev/sda1 204580 260 204320 1% /boot/efi
    /dev/mapper/vg_redhat64-lv_home
    61941748 184136 58611068 1% /home

    [root@redhat64 swapfiles]#

    My target LV to reduce is the /home directory and /dev/mapper/vg_redhat64-lv_home (a.k.a. /dev/vg_redhat64/lv_home)

  2. umount the filesystem when shrinking it

    [root@redhat64 sbin]# umount /home
    [root@redhat64 sbin]#

  3. Pre-Check the LV size (optional)

    [root@redhat64 sbin]# lvdisplay /dev/vg_redhat64/lv_home
    --- Logical volume ---
    LV Path /dev/vg_redhat64/lv_home
    LV Name lv_home
    VG Name vg_redhat64
    LV UUID EBoP2b-z465-mZwV-Y2dT-hnW5-PJxy-vZ3SNL
    LV Write Access read/write
    LV Creation host, time redhat64, 2014-02-19 05:02:40 -0500
    LV Status available
    # open 0
    LV Size 60.02 GiB
    Current LE 15364

    Segments 1
    Allocation inherit
    Read ahead sectors auto
    - currently set to 256
    Block device 253:2

    [root@redhat64 sbin]#

  4. Need to e2fsck it first

    [root@redhat64 sbin]# e2fsck -f /dev/vg_redhat64/lv_home
    e2fsck 1.41.12 (17-May-2010)
    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/vg_redhat64/lv_home: 11/3940352 files (0.0% non-contiguous), 293333/15732736 blocks
    [root@redhat64 sbin]#

  5. The filesystem is 61gig. I will reduce the FS to 49G
    REMEMBER to resize the FS first, and reduce the FS more than you reduce the LV

    [root@redhat64 sbin]# resize2fs -p /dev/vg_redhat64/lv_home 49G
    resize2fs 1.41.12 (17-May-2010)
    Resizing the filesystem on /dev/vg_redhat64/lv_home to 12845056 (4k) blocks.
    Begin pass 3 (max = 400)
    Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    The filesystem on /dev/vg_redhat64/lv_home is now 12845056 blocks long.

    [root@redhat64 sbin]#

  6. Now resize the LV.
    REMEMBER to resize the FS first, and reduce the FS more than you reduce the LV

    [root@redhat64 sbin]# lvreduce -L -10G /dev/vg_redhat64/lv_home
    WARNING: Reducing active logical volume to 50.02 GiB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce lv_home? [y/n]: y
    Reducing logical volume lv_home to 50.02 GiB
    Logical volume lv_home successfully resized
    [root@redhat64 sbin]#

  7. Post-Check the LV size (optional)

    [root@redhat64 sbin]# lvdisplay /dev/vg_redhat64/lv_home
    --- Logical volume ---
    LV Path /dev/vg_redhat64/lv_home
    LV Name lv_home
    VG Name vg_redhat64
    LV UUID EBoP2b-z465-mZwV-Y2dT-hnW5-PJxy-vZ3SNL
    LV Write Access read/write
    LV Creation host, time redhat64, 2014-02-19 05:02:40 -0500
    LV Status available
    # open 0
    LV Size 50.02 GiB
    Current LE 12804
    Segments 1
    Allocation inherit
    Read ahead sectors auto
    - currently set to 256
    Block device 253:2

    [root@redhat64 sbin]#

  8. You should now see free VG space in vgdisplay

  9. [root@redhat64 sbin]# vgdisplay
    --- Volume group ---
    VG Name vg_redhat64
    System ID
    Format lvm2
    Metadata Areas 1
    Metadata Sequence No 5
    VG Access read/write
    VG Status resizable
    MAX LV 0
    Cur LV 3
    Open LV 2
    Max PV 0
    Cur PV 1
    Act PV 1
    VG Size 135.53 GiB
    PE Size 4.00 MiB
    Total PE 34696
    Alloc PE / Size 32136 / 125.53 GiB
    Free PE / Size 2560 / 10.00 GiB

    VG UUID EsUidy-11mQ-82ma-E0IK-9W0V-2h5S-fn26xI

    [root@redhat64 sbin]#

DONE!!

HUZZAH!!

Geek, Linux

3 thoughts on “Reducing Filesystem FS Size and LVM Size in Linux

Leave a Comment

Your email address will not be published. Required fields are marked *

Spam protection by WP Captcha-Free