TECHIES WORLD

For Techs.... Techniques.... Technologies....

BashLinux

How to increase the lvm partition after increasing the harddisk

This article explains the detailed steps to increase the lvm partition after increasing the harddisk. Please note that the values and parameters are used as per the server we have used.

Step1: Login to the server via ssh as root

Step2: Check the current disk layout.

#lsblk 

NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0                        11:0    1   969M  0 rom  
vda                       252:0    0    50G  0 disk 
├─vda1                    252:1    0     1M  0 part 
├─vda2                    252:2    0     1G  0 part /boot
└─vda3                    252:3    0    49G  0 part 
    └─ubuntu--vg-ubuntu--lv 253:0    0    20G  0 lvm  /

Here the harddisk have 50G of size in total and we can extend the root partition from 20G to 49G.

Step3: Check the current status of physical volume.

#pvdisplay 

--- Physical volume ---
PV Name               /dev/vda3
VG Name               ubuntu-vg
PV Size               <20.00 GiB / not usable 0   
Allocatable           yes 
PE Size               4.00 MiB
Total PE              5120
Free PE               0
Allocated PE          5120
PV UUID               gDy04i-tLUC-c2o2-q6Ka-x5YY-3SoS-62TJAE

The result showing that the physical volume have 20.00 GiB size only and we need to resize it to utilize the full harddisk.

Step4: Resize the physical volume.

#pvresize /dev/vda3

Physical volume "/dev/vda3" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

Step5: Recheck the physical volume status.

#pvdisplay

--- Physical volume ---
PV Name               /dev/vda3
VG Name               ubuntu-vg
PV Size               <49.00 GiB / not usable 16.50 KiB
Allocatable           yes (but full)
PE Size               4.00 MiB
Total PE              12543
Free PE               0
Allocated PE          12543
PV UUID               gDy04i-tLUC-c2o2-q6Ka-x5YY-3SoS-62TJAE

Now the physical volume is using the full harddisk space.

Step6: Check the volume group and logical volume details.

#vgdisplay -v

--- Volume group ---
VG Name               ubuntu-vg
System ID             
Format                lvm2
Metadata Areas        1
Metadata Sequence No  3
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               1
Max PV                0
Cur PV                1
Act PV                1
VG Size               <49.00 GiB
PE Size               4.00 MiB
Total PE              12543
Alloc PE / Size       5120 / 20.00 GiB
Free  PE / Size       7423 / <29.00 GiB
VG UUID               0P2xAh-5wse-VmpC-I4cE-7grc-EJjC-VZf3q0

--- Logical volume ---
LV Path                /dev/ubuntu-vg/ubuntu-lv
LV Name                ubuntu-lv
VG Name                ubuntu-vg
LV UUID                JVrBEi-dQje-ueWw-Mcns-wtvr-1fNN-AYiw2v
LV Write Access        read/write
LV Creation host, time ubuntu-server, 2021-10-25 11:08:54 +0000
LV Status              available
# open                 1
LV Size                20.00 GiB
Current LE             5120
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

--- Physical volumes ---
PV Name               /dev/vda3     
PV UUID               gDy04i-tLUC-c2o2-q6Ka-x5YY-3SoS-62TJAE
PV Status             allocatable
Total PE / Free PE    12543 / 7423

Here we can see that "29.00 GiB" is free and we can extend the logical volume "/dev/ubuntu-vg/ubuntu-lv".

Step7: Extend the logical volume.

#lvextend -L+29G /dev/ubuntu-vg/ubuntu-lv

Size of logical volume ubuntu-vg/ubuntu-lv changed from <20.00 GiB (5120 extents) to <49.00 GiB (12543 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.

Step8: Recheck whether the changes reflected.

#vgdisplay -v

--- Volume group ---
VG Name               ubuntu-vg
System ID             
Format                lvm2
Metadata Areas        1
Metadata Sequence No  5
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               1
Max PV                0
Cur PV                1
Act PV                1
VG Size               <49.00 GiB
PE Size               4.00 MiB
Total PE              12543
Alloc PE / Size       12543 / <49.00 GiB
Free  PE / Size       0 / 0   
VG UUID               0P2xAh-5wse-VmpC-I4cE-7grc-EJjC-VZf3q0

--- Logical volume ---
LV Path                /dev/ubuntu-vg/ubuntu-lv
LV Name                ubuntu-lv
VG Name                ubuntu-vg
LV UUID                JVrBEi-dQje-ueWw-Mcns-wtvr-1fNN-AYiw2v
LV Write Access        read/write
LV Creation host, time ubuntu-server, 2021-10-25 11:08:54 +0000
LV Status              available
# open                 1
LV Size                <49.00 GiB
Current LE             12543
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

--- Physical volumes ---
PV Name               /dev/vda3     
PV UUID               gDy04i-tLUC-c2o2-q6Ka-x5YY-3SoS-62TJAE
PV Status             allocatable
Total PE / Free PE    12543 / 0

Here we can see that the logical volume resized to 49.00 GiB.

Step9: Resize the root partition.

#resize2fs /dev/ubuntu-vg/ubuntu-lv

resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 7
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 12844032 (4k) blocks long.

Step10: Check the filesystem usage and verify.

#df -h

Filesystem                         Size  Used Avail Use% Mounted on
udev                               461M     0  461M   0% /dev
tmpfs                               99M  1.1M   98M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   49G  7.3G   39G  16% /
tmpfs                              493M     0  493M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              493M     0  493M   0% /sys/fs/cgroup
/dev/loop3                          33M   33M     0 100% /snap/snapd/13640
/dev/vda2                          976M  150M  759M  17% /boot

That's all…