Tuesday, July 7, 2015

RHEL Logical Volume Manager - Increasing/Decreasing Size

LVM, logical volumes size can be increased or decreased depends on our needs. Logical volume can be shirked/reduced using “lvreduce” command but this will not reduce the filesystem. You need to reduce the filesystem before reducing the logical volume. Logical volume can be extended/increased using “lvextend” command.Once you have extended the logical volume ,then you need to increase the filesystem to the see new space on the mount points.
Most of the Linux/Unix beginners will confuse with term ‘volume’ and ‘filesystem’. Volume is a raw partition like a physical disk.In a order to store data on volume,you need a filesystems on that.(Ex:ext2,ext3,ext4…etc..)


1.Extending the Logical Volume size

Logical volumes can be increased on fly without un-mounting the filesystem. Here we will see how to increase the linear filesystem and volume on Redhat Linux 6.
1.Check the volume group free space before proceeding to the volume expansion.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  uavg        3   1   0 wz--n-  5.99g 5.89g
  vg_mylinz   1   2   0 wz--n- 19.51g    0
[root@mylinz ~]#
#
From the above output ,we can see volume group “uavg” has 5.89GB free space.


2.We are planning to extend volume “lvol0″ from uavg volumegroup. 
[root@mylinz ~]# lvs
  LV      VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lvol0   uavg      -wi-ao 100.00m
  lv_root vg_mylinz -wi-ao  16.54g
  lv_swap vg_mylinz -wi-ao   2.97g
[root@mylinz ~]# df -h /vol1/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                       97M  5.6M   87M   7% /vol1
[root@mylinz ~]# lvdisplay /dev/uavg/lvol0
  --- Logical volume ---
  LV Name                /dev/uavg/lvol0
  VG Name                uavg
  LV UUID                0rtjKO-3GdM-CeOf-CY8m-fZgH-wjgu-kJivtd
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                100.00 MiB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]#

3.Extend the logical volume by 100MB.
[root@mylinz ~]# lvextend -L +100M /dev/uavg/lvol0
  Extending logical volume lvol0 to 200.00 MiB
  Logical volume lvol0 successfully resized
[root@mylinz ~]# lvdisplay /dev/uavg/lvol0
  --- Logical volume ---
  LV Name                /dev/uavg/lvol0
  VG Name                uavg
  LV UUID                0rtjKO-3GdM-CeOf-CY8m-fZgH-wjgu-kJivtd
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                200.00 MiB
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]#

4.Check the mountpoint “/vol1″ .The new space is not be reflected there because,Since you didn’t resize the filesystem.
[root@mylinz ~]# df -h /vol1/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                       97M  5.6M   87M   7% /vol1
[root@mylinz ~]#

5.Increase the filesystem.
[root@mylinz ~]# resize2fs /dev/uavg/lvol0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/uavg/lvol0 is mounted on /vol1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/uavg/lvol0 to 204800 (1k) blocks.
The filesystem on /dev/uavg/lvol0 is now 204800 blocks long.

[root@mylinz ~]#

6.Check the mountpoint now.
[root@mylinz ~]# df -h /vol1
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                      194M  5.6M  179M   3% /vol1
[root@mylinz ~]#
Wow..We have successfully increased the vol1 from 100MB to 200MB.
The same way you can increase any type of volume layouts.

2.Reducing the Logical Volume size

Volumes can’t be  reduced on the fly.To reduce the volume ,
1. umount the filesystem
2. Reduce the filesystem size 
3. Reduce the logical volume
4. Mount the volume .


Here we will see how to reduce the volume “lvol0″ which is part of volume group “uavg” .

1.List he volume information.
[root@mylinz ~]# lvs
  LV      VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lvol0   uavg      -wi-ao 200.00m
  lv_root vg_mylinz -wi-ao  16.54g
  lv_swap vg_mylinz -wi-ao   2.97g
[root@mylinz ~]# lvdisplay /dev/uavg/lvol0
  --- Logical volume ---
  LV Name                /dev/uavg/lvol0
  VG Name                uavg
  LV UUID                0rtjKO-3GdM-CeOf-CY8m-fZgH-wjgu-kJivtd
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                200.00 MiB
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]# df -h /vol1
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                      194M  5.6M  179M   3% /vol1
[root@mylinz ~]#

2.un-mount the filesystem.
[root@mylinz ~]# umount /vol1
[root@mylinz ~]#

3.Reduce the filesystem by 100MB. The current filesystem size is 200MB.
Option 100MB – Final size of the volume.
[root@mylinz ~]# resize2fs /dev/uavg/lvol0 100M
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/uavg/lvol0' first.
[root@mylinz ~]#
“resize2fs” required to check the filesystem consistency before reduce the FS.
[root@mylinz ~]# e2fsck -f /dev/uavg/lvol0
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/uavg/lvol0: 11/49400 files (9.1% non-contiguous), 11884/204800 blocks
[root@mylinz ~]# resize2fs /dev/uavg/lvol0 100M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/uavg/lvol0 to 102400 (1k) blocks.
The filesystem on /dev/uavg/lvol0 is now 102400 blocks long.
[root@mylinz ~]#
We have successfully reduced  the filesystem to 100MB.

4.De-activate the volume.
[root@mylinz ~]# lvchange -an /dev/uavg/lvol0

5.Reduce the volume. “-L” – Final size of the volume.
Here i am giving volume final size as 104MB. So that  volume may have some room and  won’t touch the filesystem part.The extra 4MB can be recovered using resize2fs. Its up to you how you want to proceed.
Note:You can also give 100MB. For safer side, its better to give some extra space on the volume level.
Having good backup is recommeded before proceeding to volume resize.
[root@mylinz ~]# lvreduce -L 104M /dev/uavg/lvol0
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 104.00 MiB
  Logical volume lvol0 successfully resized
[root@mylinz ~]#

6.Activate the volume and Mount it.
[root@mylinz ~]# lvchange -ay /dev/uavg/lvol0
[root@mylinz ~]# mount -t ext4 /dev/uavg/lvol0 /vol1
[root@mylinz ~]# df -h /vol1
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                       97M  5.6M   87M   6% /vol1
[root@mylinz ~]#


Hope now you are familiar with volume re-sizing operation on Logical volume manager.

No comments:

Post a Comment