Expanding LVM with new harddisk

In this post I will describe how to expand a LVM with adding a new harddisk. This for example on a running VMware Virtual Machine.

So after this post you will have 2 disks running in 1 LVM.

Notice! Make sure to take a backup and snapshot your machine, just in case…

Confirm running Partition type

First of all confirm that you are working with an LVM.

# fdisk -l

lvm1

 

 

 

As you can see we have a “Linux LVM” listed as 8e (HEX code for an LVM).

Add the new Harddisk

Now we know that it is an LVM, you can start adding a new harddisk. Just create a new harddisk in VMware and select the right amount of space you would like to add.

Detecting new disk

Now that we have added a new harddisk it is time to verify we have a new disk. If you now run:

# fdisk -l

If you do not see the new disk (/dev/sdb) then you might have not rebooted your machine. Try running the following command:

# echo “- – -” > /sys/class/scsi_scsi_host/host0/scan

If you now run the fdisk command again you should see the new /dev/sdb disk with the message “Doesn’t contain a valid partition table”.

Partition the new disk

Now we have the new disk ready for use, we  can start making a partition on it by using fdisk.

# fdisk /dev/sdb

This should give  you the message: (the commands are highlighted in bold

root@ws-01:~# fdisk /dev/sdb
Command (m for help): n

Now use the following commands

for creating a nw partition

As we now have a new partition we need to give it a number. Because it is a new disk we give it the number 1

Partition number

Next we have 2 questions about the First and Last cylinder. You can hit ENTER key twice to confirm the defaults.

First Cylinder: “enter
Last Cylinder: “enter

Now we need to change the partitions system ID.

Command (m for help): t
selected partition 1

Now we need to enter the HEX code to confirm it is an LVM partition.

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

Now we need to write these changes to the disk and all will be saved.

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

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

If you now run “fdisk -l” you will see there is an /dev/sdb1 listed with Linux LVM as system.

Increase the logical volume

Now we will use the pvcreate command to create a physical volume for use by the LVM. We issue this command on the /dev/sdb1 we just created.

root@ws-01:~# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” succesfully created

Now we have created a physical volume we have to check the current name of our Volume Group. We can do this with vgdisplay. Just watch for the VG Name

root@ws-01:~# vgdisplay 
— Volume group —
VG Name     vg_ws01
VG Size        10GiB

Now we are going to extend the Volume Group with vgextend.

root@ws-01:~# vgextend vg_ws01 /dev/sdb1
Volume group “vg_ws01” successfully extended

If we now issue the pvscan command we can see the new disk. Just run pvscan

root@ws-01:~# pvscan 

This should give you also the new disk /dev/sdb1 and the current disk (in my case /dev/sda2).

Next we are going to increase the Logical Volume so the new space is added to the LV group.

First confirm the name / path of the Logical Volume by using the lvdisplay command

root@ws-01:~# lvdisplay

Check for the LV Name and LV Path

in my case the path was: /dev/vg_ws01/lv_root
We want to increase this by adding the new disk.

root@ws-01:~# lvextend /dev/vg_ws01/lv_root /dev/sdb1

This should give you the output that the Logical Volume root successfully resized. If you now run VGDISPLAY you wil see the new size of the Volume Group.

However if you run the df -h command you won’t see the new space! We first have to resize the file system by using the resize2fs command.

root@ws-01:~# resize2fs /dev/vg_ws01/lv_root

if you now run the df -h command you will see the new space.

Summary

We now have increased the disk space for the virtual machine by adding a new harddisk through VMware and then added the space to the Volume Group. As we did this we dont need a reboot and this can be very usefull in production environments that can not afford any downtime.

Comments are closed.