Menu

How to increase the size of a Linux LVM by adding a new disk

How to increase the size of a Linux LVM by adding a new disk.

Increase size of a Linux LVM by adding new disk:

Physical Volume (PV): Consists of Raw disks or RAID arrays or other storage devices.
Volume Group (VG): Combines the physical volumes into storage groups.
Logical Volume (LV): VG’s are divided into LV’s and are mounted as partitions.

Scenario:

Currently server has onlyl 1 HDD with 20 GB
We have created a LVM Named as – Mega-root

Existing HDD will be added into server as /dev/sda

#fdisk -l

Disk /dev/sda: 20 GB, 21474836480bytes
255 heads, 63 sectors/track, 2610 cylenders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal) : 512 bytes / 512 bytes
Disk identifier: 0x0009b218

Device Boot Start End Blocks Id System
/dev/sda1 * 1 32 40131 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 32 2611 20719617 5 Extended
/dev/sda5 32 2611 20719616 8e Linux LVM

As you can see in the above image /dev/sda5 is listed as “Linux LVM” and it has the ID of 8e.
The 8e hex code shows that it is a Linux LVM, while 83 shows a Linux native partition.
Now that we have confirmed we are working with an LVM we can continue.
For increasing the size of a Linux native partition (hex code 83)

#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/Mega-root 19G 712M 17G 4% /
tempfs 502M 0 502M 0% /lib/init/rw
tempfs 502M 0 502M 0% /dev/shm
/dev/sda1 228M 16M 201M 8% /boot

Notice above, /dev/mapper/Mega-root is the volume made up from /dev/sda5. This is what we will be expanding.

High Level steps to achieve this :

1. Need to add new disk to the VM. New disk is of 20GB. Then Reboot the server.
As soon as new disk is added (as /dev/sdb) it will not have a valid partition table.
2. Create partition on the new disk. (/dev/sdb1)
3. Create a new Physical Volume (/dev/sdb1) for later use by the LVM.
4. Check the VG Name and extend the VG with new physical volume.
5. run pvscan. This will show the original physical volume and newly created physical volume.
6. Extend the required logical volume (LV) with new physical physical volume.
7. last step to resize2fs or xfs_growfs to extend the partition.

Detailed Steps:

1. Add new HDD to the VM

Reboot the system, and you will see like below after reboot:

#fdisk -l

Disk /dev/sda: 20 GB, 21474836480bytes
255 heads, 63 sectors/track, 2610 cylenders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal) : 512 bytes / 512 bytes
Disk identifier: 0x0009b218

Disk /dev/sdb doesn’t contain a valid partition table

2. Create partition on the new disk

#fdisk /dev/sdb
root@:~# fdisk /dev/sdb
Command (m for help): n // n for adding new partition.

Command action
e extended
p primary partition (1-4)
p // p for adding new partition.

As this is a new disk, we do not yet have any partitions on it so we will use partition 1 here.

Partition number (1-4): 1

Next we press the enter key twice, as by default the first and last cylinders of the unallocated space should be correct.

First cylinder (1-2610, default 1): “enter”
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): “enter”
Using default value 2610

‘t’ is selected to change to a partitions system ID, in this case we change to ’1′ automatically as this is currently our only partition.

Command (m for help): t
Selected partition 1

The hex code ’8e’ was entered as this is the code for a Linux LVM which is what we want this partition to be, as we will be joining it with the original Linux LVM which is currently using /dev/sda5.

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

‘w’ is used to write the table to disk and exit, all changes that have been done will be saved and then you will be exited from fdisk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

By using “fdisk -l” now you will be able to see that /dev/sdb1 is listed, this is the new partition created on our newly added /dev/sdb disk and it is currently using all 20gb of space.

#fdisk -l

Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 8e Linux LVM

3. Create a new Physical Volume (/dev/sdb1) for later use by the LVM.

Next we will use the pvcreate command to create a physical volume for later use by the LVM.
In this case the physical volume will be our new /dev/sdb1 partition.

root@:~# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created

4. Check the VG Name and extend the VG with new physical volume.
Now using the vgextend command, we extend the ‘Mega’ volume group by adding in the physical volume of /dev/sdb1 which we created using the pvcreate command just before.

root@:~# vgdisplay
— Volume group —
VG Name Mega

VG Size 19.76 GiB

root@:~# vgextend Mega /dev/sdb1
Volume group “Mega” successfully extended

5. run pvscan. This will show the original physical volume and newly created physical volume.

Using the pvscan command we scan all disks for physical volumes,
this should confirm the original /dev/sda5 partition and the newly created physical volume /dev/sdb1

root@:~# pvscan
PV /dev/sda5 VG Mega lvm2 [19.76 GiB / 0 free]
PV /dev/sdb1 VG Mega lvm2 [19.99 GiB / 19.99 GiB free]
Total: 2 [39.75 GiB] / in use: 2 [39.75 GiB] / in no VG: 0 [0 ]

6. Extend the required logical volume (LV) with new physical physical volume.

Next we need to increase the logical volume with the lvextend command (rather than the physical volume which we have already done).
This means we will be taking our original logical volume and extending it over our new disk/partition/physical volume of /dev/sdb1.

Firstly confirm the name of the logical volume using lvdisplay. The name will vary depending on your setup.

root@Mega:~# lvdisplay
— Logical volume —
LV Name /dev/Mega/root
LV Size 18.91 GiB

The logical volume is then extended using the lvextend command. We are extending the original logical volume of /dev/Mega/root over the newer /dev/sdb1

root@Mega:~# lvextend /dev/Mega/root /dev/sdb1
Extending logical volume root to 38.90 GiB
Logical volume root successfully resized

If you like you can then run vgdisplay and lvdisplay again to confirm the size of the volume group and logical volume respectively.

7. last step to resize2fs or xfs_growfs to extend the partition.

However if you run a “df” command to see available disk space it will not have changed yet as there is one final step, we need to resize the file system using the resize2fs command in order to make use of this space.

Alternatively if you’re running the XFS file system (default as of RedHat/CentOS 7) you can grow the file system with “xfs_growfs /dev/Mega/root

Loading

Categories:   Linux

Comments