Extend Partition in Linux
Linux

How to Extend a Partition in Linux

In this article discuss about Extend a Partition in Linux Machine without reboot the machine using Logical Volume Manager.

Do you frequently experience situations when your partition gets full, and you’re concerned whether you should remove some data or switch to a new VM? Therefore, moving to a different VM is really not necessary. Using LVM, you can easily increase the disk space.

In the virtual machine, increase the storage space and then take a snapshot of the virtual machine after the increase in storage space.

The kernel LVM module lvm-mod.o allows us to use LVM by grouping block devices into a single larger device that can be managed as one.

 

⇒ what is extended partition in Linux

An extended partition allows you to create more partitions than the four allowed by dividing up a primary partition into logical partitions.

Step1: Create a new partition disk:

I need to extend root partition from 35GB to 41GB. firstly, Increase the disk space on virtual machine level as per your requirement.

Current root partition size is 35GB

Current root partition size is 35GB

  • In general, after increasing the size of hard disk on VM level need to reboot the machine to visible in the operating system, however without rebooting your machine, run the following commands to check extended amount of disk size.

echo 1 > /sys/class/block/sd?/device/rescan

NOTE:  Please replace the? with (sda) Our example uses sda for the disk as shown below

echo 1 > /sys/class/block/sda/device/rescan

 

Create new partition disk by using fdisk utility as shown below 

Run fdisk /dev/sda to create a new partition.

Create new partition for LVM extend

Create new partition for LVM extend

NOTE: In order to extend your partition with LVM, your filesystem must be Linux LVM (type 8e)

 

Now Apply partprobe command to avoid reboot.

partprobe

⇒ Extend Partition in Linux using LVM

An important Linux admin task is to resize the file system size. Linux provides the facility to do this through LVM.As part of our discussion of lvextend, we will learn how to extend LVM partitions on the fly by using the command lvextend.

Step2: Initializes Physical Volume on a device

Now create Physical Volume PV create by using below syntax

pvcreate /dev/name of the disk

in this example it is pvcreate /dev/sda3

you could see details in  lsblk command as shown below 6GB Free space at sda3

[root@C7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 46G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 39G 0 part
│ ├─centos-root 253:0 0 35G 0 lvm /
│ └─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─sda3 8:3 0 6G 0 part
sr0 11:0 1 1024M 0 rom

[root@C7 ~]#
[root@C7 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <39.00g 0

[root@C7 ~]#
[root@C7 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
[root@C7 ~]#

[root@C7 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <39.00g 0
/dev/sda3 lvm2 --- 6.00g 6.00g

 

Step3: Extend the Volume Group with the new /dev/sda3

In actuality, extending a volume group means adding a new Physical Volume.

Below is the syntax to Extending a Volume Group(vg)

vgextend <your VG name> <your PV name>

In this example VG name is centos as shown in vgs command and PV name is /dev/sda3

[root@C7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <39.00g 0

[root@C7 ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
[root@C7 ~]#

[root@C7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 44.99g <6.00g

 

Extend the logical volume for root partition:

The size of LV (Logical Volume) can be increased online with no downtime.

Firstly, check existing logical volume status using below command

lvs

you could see root partition size is 35GB and will extended the 100% free space partition to existing Logical Volume.

lvextend –L +100%FREE </dev/your vg name /your lv name>

In this example vg name is /dev/centos and lv name is root.

below is the output

[root@C7 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <35.00g
swap centos -wi-ao---- 4.00g


[root@C7 ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <35.00 GiB (8959 extents) to 40.99 GiB (10494 extents).
Logical volume centos/root successfully resized.

[root@C7 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 40.99g
swap centos -wi-ao---- 4.00g

Resize the filesystem:

If you observer first screenshot root partition file system type is xfs , so use below command

xfs_growfs /dev/your vg name /your lv name

if you’re using   ext4 use below command

resize2fs /dev/ vg name / lv name

Now perform the activity

[root@C7 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 40.99g
swap centos -wi-ao---- 4.00g

[root@C7 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 9.5M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 35G 4.7G 31G 14% /
/dev/sda1 xfs 1014M 185M 830M 19% /boot
tmpfs tmpfs 783M 12K 783M 1% /run/user/42
tmpfs tmpfs 783M 0 783M 0% /run/user/0


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


[root@C7 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 9.5M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 41G 4.7G 37G 12% /
/dev/sda1 xfs 1014M 185M 830M 19% /boot
tmpfs tmpfs 783M 12K 783M 1% /run/user/42
tmpfs tmpfs 783M 0 783M 0% /run/user/0

 

Conclusion:

Extend Partition in Linux has been completed successfully. Procedure is same for RHEL & CentOS version makes it easy to extend LVM partitions. As per above example successfully extended root partition from 35GB to 41GB.

if you have any question about Extend Partition in Linux, please feel free to contact me. In previous article discussed about verify version of centos.

Next PostHow to Delete Recovery Partition in Windows Previous PostHow to Check Centos Version

Leave a Reply

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