In this demonstration I'm going to show you how to increase the disk space for a virtual
machine running Linux that's using logical volume manager, also known as LVM. So firstly
we will increase the size of the actual disk on the virtual machine, which in this case
is the VMDK file because we're working with VMware Workstation. Once this is complete
we'll get into the virtual machine itself and make the necessary changes through the
operating system in order to use the additional space that's been provided after we increase
that hard drive. So this is going to involve creating a new partition out of this newly
found space, expanding the volume group and logical volume followed by resizing the actual
file system itself as the final step. It's important to note that before proceeding I
strongly recommend that you have a full backup of all your data because entering a command
incorrectly while working with the disk could potentially cause data loss. If you have a
virtual machine like I have make sure you have a snapshot first otherwise ensure that
you have some other form of up to date backup and check that before proceeding. I should
also note that a snapshot might not be possible until after the disk has actually been expanded,
I know in the case of VMware Workstation at least I can't actually expand the disk while
there's a snapshot in place, so the snapshot would have to come after the disk has been
expanded. In this particular example I'm using a CentOS 7 virtual machine which as I mentioned
is running in VMware Workstation, though these steps should work the same regardless of the
virtualization software that you're using. In this case the current disk size is 20 gigabytes
and we'll be looking to expand that to 30 gigabytes. As this method works with LVM
will first confirm that our partition type is actually Linux LVM with the fdisk command.
As you can see here /dev/sda2 is listed as being a Linux LVM
Which is type 8e, which is the hex code for LVM.
On the other hand we have /dev/sda1 which is just a native Linux partition
which is identified by 83, and that's just used for the /boot drive.
So here you can see /dev/sda1 is /boot. So now we'll just have a quick look at the current
disk space so we can see that the majority of the space is used on the root volume
so you can see here that /dev/mapper/centos-root is 19 gigabytes, and as I mentioned before this virtual machine
has a 20 gigabyte disk, so this is what we're going to be looking to expand, the root volume.
Now that you've seen how things are currently set up, we'll start by first expanding the virtual hard drive
and with VMware Workstation I don't seem to have this option unless I power off the virtual machine
however I've done this before with VMware vSphere and other virtualization options and those allowed me to do the expansion on the fly
so you can potentially complete the whole process with no down time required
In this test environment though I need to power off the virtual machine first
if we have a look at the settings of it and go to the disk
you'll see the expand button is basically greyed out
I'll just quickly shutdown and we'll be able to continue once that's complete
Ok so now that the virtual machine is off we can edit the settings again select the disk, and now all these options are no longer greyed out and we can actually use them
In this case, as you can see the current disk size is 20 gigabytes, we're just going to increase that to 30 gigabytes and select expand.
Now it's just going to expand the virtual disk and all this is going to do is just change the size of the actual hard disk
so it's not going to be usable within the operating system until we go in and run some commands and actually tell it to make use of that space
which is what we'll be doing once this completes and we boot back up.
Ok so now it's complete, it's showing that the disk was successfully expanded and we must re-partition the disk and expand the file system from within the guest operating system
so exactly what I just mentioned
so we have to boot it up and configure it.
So we'll just power it back on
The virtual machine has booted back up and I've logged in
As we've just booted back up after increasing the space of the actual hard disk, the new space should automatically show
when I run the fdisk -l command
as you can see it's showing the /dev/sda disk as now being 32 gigabytes I think previously it was showing 21.5
so that space is actually being picked up, we can now use that space
what we're going to do with this additional space is create a third partition /dev/sda3 and we'll be able to go from there
To first use that additional space we're going to have to create a new partition, so we're just going to use the fdisk command for that
and specify fdisk /dev/sda, which is the primary disk of this virtual machine
it's the only disk that it currently has
If you haven't used fdisk before you can press the 'm' key and it's just going to show you everything that's available that you can do with it
so that will give you a good idea of how it all works
we're going to press 'n' first for new
and we're going to want to press 'p' for a primary partition
and this is going to make it the third one as we already have two
it's worth noting here that it does note that there's already two primary and that there's two free
in total you can only have 4 primary partitions so if you already have 3 and this is going to be your 4th I'd suggest making it a logical / extended partition.
Alternatively you could add in a new virtual disk to the virtual machine rather than expanding the existing one, which is what we're doing in this one
I'll put some links in the description on how you can do it the other way
but for the time being we do have the space so we're just going to continue on with extending the existing disk
We're going to make this partition 3 which is the default option so just press enter
and now it's asking where I want to start this partition
we're just going to press enter, so by default it's going to use the lowest position available
and for the last it's again going to give you the last available option
which is fine because we want to make use of all the disk space which we've just added
we're not going to do anything specific
so now that that's done, we need to set the type of partition, as you can see above that's done with the letter 't'
for change partition system ID, so we'll press 't'
and it wants us to select the partition that we're going to set, so we're going to use 3 because we just created our new partition as number 3
now we need to give it the hex code, so there are a fair few hex codes as you can see here
by entering the capital 'L'
we want to use 8e which is Linux LVM
so as you can see it's changed partition Linux to Linux LVM
Now we're basically done here, so to save all our changes you just need to press 'w' and then enter
so that's going to actually write the changes, if you don't actually press 'w' and write the changes
everything that we've just done previously in fdisk wont be saved or it wont apply at all
so make sure that you write changes
you might see a warning here like we do
basically showing that the partition table can't be updated and you might need to reboot
so after using fdisk to create the /dev/sda3 partition I did have to reboot the virtual machine first
that was basically required because it wasn't able to write the changes to the partition table while it's in use
so you can try and get around that with commands like partprobe or partx, but in this case it's just a test environment so we just rebooted it
that's all for partitioning, so just to summarize:
so far we have created the /dev/sda3 partition, which is using all of the unallocated space that we got from increasing the disk in VMware Workstation
so with this new partition we can use the pvcreate command to turn it into a physical volume
so we can do that with pvcreate /dev/sda3
so that successfully created a physical volume /dev/sda3
and we're going to use this new physical volume to increase the existing volume group
so we need to confirm that the name of the volume group that we actually want to add it to
this can be done with the vgdisplay command
As you can see the first field is VG Name is just centos, so in this case our one and only volume groups name is centos
It's also worth noting the free physical extent / size field is currently 0
which is because it doesn't have any free space in the volume group
which is why we are going through this process
if you had free space here already you'd just be able to use lvextend and increase your logical volume, but we don't
so what we're doing is creating that physical volume, and adding that into this volume group
so that we can then extend the logical volume
In this example as I mentioned centos is the volume group, so we're going to use the vgextend command
on that volume group
and add /dev/sda3 in
so that's done by just specifying vgextend followed by the volume group name, in this case 'centos', followed by the partition we've created
so that's /dev/sda3
as you can see volume group centos successfully extended
now if we run the vgdisplay command again
we can see that the free space is now 10 gigabytes
so that space is now in that volume group and it can be assigned to logical volumes that exist inside of that volume group
If we run a lvdisplay command...
we can see the logical volumes that we currently have, so by default we just have one for the root partition
which is what we're going to be expanding, and one for swap
so just ignore the swap one we're not going to be using that
it's important to note that both of them show the volume group name as being centos
which is where we added the physical volume into, it's possible that you might have multiple volume groups
with logical volumes inside different ones, so just make sure that you add the physical volume to the volume group name which the logical volume exists in
so for instance /dev/centos/root is part of the volume group centos, so that's why we expanded the centos volume group in this case.
Now we'll run a pvscan command, and that will just show us all of the physical volumes
as well as the volume groups they exist in, so you can see both /dev/sda2 and /dev/sda3 are both in the volume group of centos
but /dev/sda3 does show as having 10 gigabytes free because it's not yet in use
Now that we have this extra free space inside the volume group we can go ahead and expand the logical volume
which in this case is /dev/centos/root, we do that with the lvextend command
We can either use all free space in the volume group, so we could expand /dev/centos/root by the full 10 gigabytes
or we could just specify, say 5 gigabytes and then we would have 5 gigabytes free that we could assign
either in the future or to a different logical volume which might exist inside that volume group
We use lvextend -L which is basically saying we're going to extend by the amount specified
+5 gigabytes
/dev/centos/root
As you can see the size of that logical volume has been increased successfully from 19 gigabytes to 24 gigabytes
so we've increased it by 5 gigabytes as specified
Now if you run an lvdisplay again and we have a look at that root volume
we can see that the LV size is now listed successfully as 24 gigabytes
and if we run a vgdisplay we can see that the free space is 5 gigabytes
because we've taken 5 gigabytes out of the centos volume group and assigned it to that root volume
if we run a 'df -h' again we can see that the space isn't yet available for use
it's still showing the root partition as 19 gigabytes in size
and the reason for that is that we haven't yet resized the file system
which is the final step
the logical volume has been successfully increased and now the last step is to resize the actual file system which is running on top of that
so in this case we do that with the resize2fs command and specify that root volume which is /dev/centos/root
and that just performs an online resize of the file system, now if we check the space it shows as 24 gigabytes
for that root volume, it has successfully been expanded
I should also mention that in this case I created the file system as ext4
which is why we were able to use the resize2fs command here
however in CentOS 7 by default it does use the newer XFS file system
if you did have an XFS file system and resize2fs doesn't work you would be able to do this pretty similarly
you'd just use xfs_growfs and then specify /dev/centos/root
Just to summarise what we've done:
the virtual disk of the virtual machine itself was first expanded
we then used that free space to create a new LVM partition with fdisk
we turned it into a physical volume with the pvcreate command
and then extended the existing volume group over the top of the physical volume using the vgextend command
then we extended the logical volume with the lvextend command and then we resized the file system to take advantage of that
newly added space to the logical volume giving us extra usable space as shown by that final 'df -h' command