Expand linux centos7 partition

from here very easy

Steps

1. Shut down the VM from Hypervisor

 

2. Expand the disk capacity from settings with your desired value. Here we choose to expand the VHD with additional 60 GB space.

3. Start the VM from the hypervisor.

4. Login to virtual machine console as root.

5. Execute below command to check the disk space.

fdisk -l

6. Now execute this below command to initialize the expanded space and mount it.

fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

7. After this enter ‘n’ in the next line for creating new partition.

Command action
e extended
p primary partition (1-4)
p

8. Then assign the partition number you wish based on your existing partition numbering.If you have to expand existing linux space you have in anycase create a new partition /dev/sda3 or  /dev/sda4 if was already created before.

Partition number (1-4): 3

Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

9. In the next line choose code ‘8e’ for select the LINUX OS

Hex code (type L to list codes): 8e

Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

10. Next enter ‘w’ to proceed further.

Command (m for help): w

Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

11. Reboot the system.

12. Now we need to create physical & logical volumes execute below command to create physical volume.

pvcreate /dev/sda3

13. Execute below command to get the name of current Volume Group

vgdisplay

14. Execute below command to extend the Volume Group with /dev/sda3

vgextend VolGroup /dev/sda3

15. Execute below command to get Logical Volume path.

lvdisplay

16. Execute below command to extend the Logical Volume with /dev/sda3

lvextend /dev/VolGroup/lv_root /dev/sda3

or

lvextend -l+100%FREE /dev/VolGroup/lv_root

17. Execute below command to update the Logical Volume

xfs_growfs /dev/VolGroup/lv_root

18. Check for the new disk space.

>df -h

19. Your Disk space is now successfully increased with 60 GB.

Real example of increasing space :

Status disk before the physical space extension with fdisk /dev/sda > p

Disk /dev/sda: 182.5 GB, 182536110080 bytes, 356515840 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Identificativo disco: 0x000d7440

Dispositivo Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 167772159 82836480 8e Linux LVM
/dev/sda3 167772160 209715199 20971520 83 Linux
/dev/sda4 209715200 356515839 73400320 8e Linux LVM

Disk /dev/mapper/centos-root: 150.3 GB, 150319661056 bytes, 293593088 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-home: 29.0 GB, 28982640640 bytes, 56606720 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Status disk after the physical space extension with fdisk /dev/sda > p

Disk /dev/sda: 236.2 GB, 236223201280 bytes, 461373440 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Identificativo disco: 0x000d7440

Dispositivo Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 167772159 82836480 8e Linux LVM
/dev/sda3 167772160 209715199 20971520 83 Linux
/dev/sda4 209715200 356515839 73400320 8e Linux LVM

Disk /dev/mapper/centos-root: 150.3 GB, 150319661056 bytes, 293593088 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-home: 29.0 GB, 28982640640 bytes, 56606720 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

The technical steps

First step extend last partition thats means delete and recreate the primary partition in my case sda4:
(snapshot done)
fdisk /dev/sda
p > take note of beginning of sda4 just in case
d > 4
n > p > 4 (default) > >
t > 4 > 8e (to mark it as LVM partition)
w

reboot

Resize the lvm :
pvscan > to make sure we have free space
[root@foras ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<79,00 GiB / 0 free]
PV /dev/sda3 VG centos lvm2 [<20,00 GiB / 0 free]
PV /dev/sda4 VG centos lvm2 [<70,00 GiB / 0 free]
Total: 3 [<168,99 GiB] / in use: 3 [<168,99 GiB] / in no VG: 0 [0 ]

[root@foras ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 220G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 79G 0 part
│ ├─centos-root 253:0 0 140G 0 lvm /
│ ├─centos-swap 253:1 0 2G 0 lvm [SWAP]
│ └─centos-home 253:2 0 27G 0 lvm /home
├─sda3 8:3 0 20G 0 part
│ └─centos-root 253:0 0 140G 0 lvm /
└─sda4 8:4 0 120G 0 part
└─centos-root 253:0 0 140G 0 lvm /
sr0 11:0 1 1024M 0 rom

[root@foras ~]# pvdisplay
— Physical volume —
PV Name /dev/sda2
VG Name centos
PV Size <79,00 GiB / not usable 3,00 MiB
Allocatable yes (but full)
PE Size 4,00 MiB
Total PE 20223
Free PE 0
Allocated PE 20223
PV UUID 1p8aba-7K7p-KAyB-5GFX-zVFX-gxM8-71BlqX

— Physical volume —
PV Name /dev/sda3
VG Name centos
PV Size 20,00 GiB / not usable 4,00 MiB
Allocatable yes (but full)
PE Size 4,00 MiB
Total PE 5119
Free PE 0
Allocated PE 5119
PV UUID 3Vu6jg-Nime-AMmf-6Cby-3QDS-Pn6X-Pb5Tzn

— Physical volume —
PV Name /dev/sda4
VG Name centos
PV Size <70,00 GiB / not usable 3,00 MiB
Allocatable yes (but full)
PE Size 4,00 MiB
Total PE 17919
Free PE 0
Allocated PE 17919
PV UUID 5PExzl-pOWJ-Tyki-v47t-dh7z-erCO-IgSzP0

pvresize /dev/sda4
lvscan > to take note of lv path
lvextend -l +100%FREE /dev/centos/root

[root@foras ~]# pvresize /dev/sda4
Physical volume “/dev/sda4” changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@foras ~]# lvscan
ACTIVE ‘/dev/centos/swap’ [2,00 GiB] inherit
ACTIVE ‘/dev/centos/home’ [26,99 GiB] inherit
ACTIVE ‘/dev/centos/root’ [<140,00 GiB] inherit
[root@foras ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <140,00 GiB (35839 extents) to <190,00 GiB (48639 extents).

xfs_growfs /

[root@foras ~]# xfs_growfs /
meta-data=/dev/mapper/centos-root isize=512 agcount=12, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=36699136, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 36699136 to 49806336

[root@foras ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<79,00 GiB / 0 free]
PV /dev/sda3 VG centos lvm2 [<20,00 GiB / 0 free]
PV /dev/sda4 VG centos lvm2 [<120,00 GiB / 0 free]
Total: 3 [<218,99 GiB] / in use: 3 [<218,99 GiB] / in no VG: 0 [0 ]

history

1001 df -ha
1002 vgdisplay
1003 lvdisplay
1004 vgdisplay
1005 vgextend centos /dev/sda4
1006 lvdisplay
1007 lvextend -l+100%FREE /dev/centos/root
1008 df -ha
1009 df -h
1010 xfs_growfs centos
1011 df -h
1012 pvcreate /dev/sda4
1013 vgdispaly
1014 vgdisplay
1015 vgextend centos /dev/sda4
1016 resize2fs /dev/centos/root
1017 fsadm resize /dev/centos/root
1018 df -ha
1019 pvscan
1020 fdisk /dev/sda
1021 htop
1022 pvcreate /dev/sda4
1023 lsblk
1024 pvcreate /dev/sda4
1025 pvcreate –force /dev/sda4
1026 htop
1027 pvcreate –force –force /dev/sda4
1028 umount /dev/sda4
1029 lvchange -an /dev/centos/root
1030 pvcreate –force /dev/sda4
1031 wipefs -a /dev/sda4
1032 pvcreate –force /dev/sda4
1033 lsof /dev/sda4
1034 mount | grep /dev/sda4
1035 lvdisplay
1036 lvchange -an /dev/centos/root
1037 dmsetup remove /dev/sda4
1038 wipefs -a /dev/sda4
1039 blkid /dev/sda4
1040 partprobe /dev/sda
1041 dmsetup ls
1042 vgreduce centos /dev/sda4
1043 pvscan
1044 lvextend -l +100%FREE /dev/centos/root
1045 lsblk
1046 pvdisplay
1047 pvresize /dev/sda4
1048 lvscan
1049 lvextend -l +100%FREE /dev/centos/root
1050 df -ha
1051 xfs_growfs /
1052 df -ha
1053 history
1054 pvscan

 

 

 

Leave a Reply