This post will cover how to increase the disk space for a VMware virtual machine running Linux that is using logical volume manager (LVM).
First we will add a new disk to the virtual machine and then extend the original LVM over this additional space.
Basically we will have two physical disks but just one volume group and one logical group that is using the space on both disks together.
With this method there is no down time for the virtual machine.
Important Notes:
Be very careful when working with the commands in this article as they have the potential to cause a lot of damage to your data.
If you are working with virtual machines make sure you take a snapshot of your virtual machine before hand, or otherwise have some other form of up to date backup before proceeding.
It could also be worth cloning the virtual machine first and testing out this method on the clone.
Identifying the partition type
As this method focuses on working with LVM, we will first confirm that our partition type is actually Linux LVM by running the below command.
fdisk -l
As we 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.
Below is the disk information showing that our initial setup only has the one 20gb disk currently, which is under the logical volume named /dev/mapper/Mega-root – this is what we will be expanding with the new disk.
Note that /dev/mapper/Mega-root is the volume made up from /dev/sda5 currently – this is what we will be
Adding a new virtual hard disk
First off we add a new disk to the virtual machine.
Select create a new standard virtual disk.
Type the size that you want for the new hard disk.
Review everything and click save once you have confirmed the settings.
Detect the new disk space
As soon as we add the additional disk in VM/VMware it will be displayed through “fdisk -l”.
We can see the second disk labelled /dev/sdb (Cropped out the information on /dev/sda1).
Note that it shows as not containing a valid partition table.
This may not however be the case for you, but to avoid reboot you may need to rescan your devices, you can try this with the below command. Note that you may need to change host0 depending on your setup.
echo "- - -" > /sys/class/scsi_host/host0/scan
If we get issues detecting the new disk, we must perform a reboot and it should then display correctly.
Partition the new disk
We now need to partition the new /dev/sdb disk so that it can be used, this will be done using fdisk command.
fdisk /dev/sdb
This should provide us with the below prompt, the inputs I in entered in are shown in bold.
‘n’ was selected for adding a new partition.
root@Mega:~# fdisk /dev/sdb
Command (m for help): n
‘p’ is then selected as we are making a primary partition.
Command action
e extended
p primary partition (1-4)
p
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.
Increasing the logical volume
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@Mega:~# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
Now we need to confirm the name of the current volume group using the vgdisplay command.
The name will vary depending on your setup, for me it is the name of my test server. vgdisplay provides plenty of information on the volume group.
root@Mega:~# vgdisplay
--- Volume group ---
VG Name Mega
VG Size 19.76 GiB
Now using the vgextend command, we extend the ‘Mega’ volume group by adding in the physical volume of /dev/sdb1.
root@Mega:~# vgextend Mega /dev/sdb1
Volume group "Mega" successfully extended
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@Mega:~# 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 ]
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
You can run vgdisplay and lvdisplay again to confirm the size of the volume group and logical volume respectively.
LV Size 38.90 GiB
VG Size 39.75 GiB
However if we 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.
root@Mega:~# resize2fs /dev/Mega/root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/Mega/root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/Mega/root to 10196992 (4k) blocks.
The filesystem on /dev/Mega/root is now 10196992 blocks long.
we use the command resize2fs for etx2,3,4…
Alternatively if we have XFS file system we can grow the file system with “xfs_growfs /dev/Mega/root”.
Rather than resizing the file system manually, we can use the -r option of the lvextend command which will automatically resize the file system to make use of the additional disk space.
The resize take a bit to complete (it will depend on the disk speed and size).
Running the “df” command now shows the correct disk space for /dev/mapper/Mega-root
Good Work !
Recent Comments