Monday 29 April 2013

How to Reduce a Logical Volume in GNU/Linux Using LVM


How to Reduce a Logical Volume in GNU/Linux Using LVM

Logical Volume Management is a vast improvement over standard partitioning schemes. Among many other things, it allows you to decrease the size of a volume without recreating it completely.
All of the required steps must be performed on an unmounted volume. If want to reduce the size of a non-root volume, simply unmount it. For a root volume, you’ll have to boot from a CD. Any modern live or rescue CD should work fine
In this article I will explain how to reduce the Logical Volume in Red Hat Linux system without loss of data.

STEPS:-

1.   Un mount the logical volume: 

umount /mnt/logical_volume1

Ex-
[root@server01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root     50G   26G   22G  55% /
tmpfs                                         7.8G     0  7.8G   0% /dev/shm
/dev/sda1                                            485M   32M  429M   7% /boot
/dev/mapper/VolGroup-lv_home  491G   55G  412G  12% /home

[root@server01 ~]# umount /home
If this file system is in use, Use the following commands to unmount partition forcefully.

[root@server01 ~]# fuser –km /home
[root@server01 ~]# umount  –f /home
2.   Make a backup of logical Volume.

Steps:
  1. Check the size of the logical volume (LV Size): 
lvdisplay /dev/volume_group1/logical_volume1


Ex- 

--- Logical volume ---
LV Name /dev/mapper/VolGroup-lv_home
VG Name VolGroup
LV UUID AxihqP-Yt8l-5scY-bXNG-Bn5D-K3ms-X7v1Ys
LV Write Access read/write
LV Status available
# open 1
LV Size 200,00 GB
Current LE 6400
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:5

Usually, snapshots are smaller than the original logical volume, but I recommend to use at least the same space.
  1. Create the snapshot: 
lvcreate -L200G -s -n backup_logical_volume1 /dev/volume_group1/logical_volume1

The -s param tells lvcreate to create a snapshot instead of a normal logical volume.

Ex-

lvcreate -L200G -s -n backup_logical_volume  /dev/mapper/VolGroup-lv_home

3.   Check the file system integrity: 

fsck -f -y -v /dev/volume_group1/logical_volume1

Ex-
[root@server01 ~]#   fsck -f -y -v /dev/mapper/VolGroup-lv_home
fsck from util-linux-ng 2.17.2
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

 2634206 inodes used (8.06%)
   62765 non-contiguous files (2.4%)
       7 non-contiguous directories (0.0%)
         # of inodes with ind/dind/tind blocks: 0/0/0
         Extent depth histogram: 2634192/4
16246730 blocks used (12.43%)
       0 bad blocks
       3 large files

 2589225 regular files
   44972 directories
       0 character device files
       0 block device files
       0 fifos
       0 links
       0 symbolic links (0 fast symbolic links)
       0 sockets
--------
 2634197 files


4.   Resize the file system to something smaller than the final size (around 190GB in this case): 

resize2fs /dev/volume_group1/logical_volume1 190G

This Step takes a long time to resize the volume.

Ex-

[root@server01 ~]# resize2fs /dev/mapper/VolGroup-lv_home 190G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 49807360 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_home is now 49807360 blocks long.

5.   Check the filesystem integrity again: 

fsck -f -y -v /dev/volume_group1/logical_volume1

Ex-

fsck -f -y –v /dev/mapper/VolGroup-lv_home

6.   Reduce the logical volume: 

lvreduce -L 85G /dev/volume_group1/logical_volume1

Ex-

[root@server01 ~]# lvreduce -L 195G /dev/mapper/VolGroup-lv_home
  WARNING: Reducing active logical volume to 195.00 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 195.00 GiB
  Logical volume lv_home successfully resized


7.   Resize the filesystem to fit the logical volume: 

resize2fs /dev/volume_group1/logical_volume1

Ex-

[root@server01 ~]# resize2fs /dev/mapper/VolGroup-lv_home
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 51118080 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_home is now 51118080 blocks long.

8.   Check the filesystem to know whether the reduction went fine:

 fsck -f -y -v /dev/volume_group1/logical_volume1

Ex-
fsck -f -y –v /dev/mapper/VolGroup-lv_home


9.   Check the size of Logical Volume.

[root@server01 ~]# lvs

  LV      VG       Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  lv_home VolGroup -wi-a--- 195.00g
  lv_root VolGroup -wi-ao--  50.00g
  lv_swap VolGroup -wi-ao--   7.84g


10.                Mount the logical volume: 

mount /mnt/logical_volume1

Ex-

mount  /dev/mapper/VolGroup-lv_home  /home



NOTE: if you aren’t careful with the disk space you are using and the final disk space you are going to be using, this operation can cause data loss. I recommend to backup the logical volume before the reduction and to use around 5GB of “safe space” while reducing the filesystem in order to avoid data loss.

2 comments:

  1. Pada bhi he apna blog..... Kuch samaj nahi aa ya

    ReplyDelete
    Replies
    1. Kya samajh mein nahi aaya. Sab saaf saaf to likha hai and practical karke hi likha hai.

      Delete