Expanding the Size of Disk Volume on a Hyper-V Linux Guest.
Oh no I've run out of space on my Linux Guest
If you're a Microsoft specialist and you've been setting up Hyper-V servers with Windows Guests then your more than likely come across the scenario of running out of disk space on your virtual guests. Now for Windows guests the task of expanding the virtual guests disk size is pretty easy but if you need help with that see my other hub - How to increase the size of my Hyper-V Windows Guest Disk.
Now you may have decided to run a couple of Linux systems on your Hyper-V host and because they work quite well you've also run out of space on one of those too. So how on earth do we increase the disk space on one of these machines.
Hyper-V Edit Disk Menu
Expanding the Virtual Hard Disk (VHD) File
Our first step is obviously to increase the size of the VHD File for that particular Linux Guest, just like you would if it were a Windows Guest. Make sure you shut down the Linux Guest before you start and I would advise taking a copy of the VHD file before we begin as a backup.
Next open up your Hyper-V manager and click on Edit Disk on the right hand side under Actions. On the Welcome screen just click next then click the Browse button and find your VHD file for your Linux Guest. Click next again and choose Expand from the 3 options and click next. Now you want to enter the new size that you would like to make the hard disk. Click next when you are done and confirm the summary details are correct and click finish.
Now you're going to need to wait while the VHD file is Expanded this can take quite some time depending on how much bigger you have made the disk.
Linux LVM Partitions in CFDISK
Extending the Extended Partition and adding a Logical Partition
Confused yet, well I was until I found this great tool called cfdisk which makes this job a lot easier. So now that you've expanded the VHD file you can boot the Linux Guest again and log in. Next we are going to use a program called cfdisk, now your server may not have this yet so you'll have to install it with either YUM or APT-GET.
Now that CFDISK is installed we simply type
Depending on your Linux Distribution.
You will then be presented with a screen similar to the image above.
You should see the space that you just added to the VHD file as Free Space at the end of your Disk. Now what we want to do is add a new Logical Partition to the HDA/SDA disk. So use the cursor to highlight the Free Space at the bottom of the list and choose New from the bottom menu. Next choose Logical for the partition type and enter the size you would like to make it then press enter. This will create a new logical partition with the File System type of Linux, next we need to change this to a Linux LVM File System Type. So choose Type from the bottom menu, press enter to continue, then enter 8e as the Filesystem Type. Next we just need to write the new Partition Table out, so choose Write from the Menu and confirm.
Linux LVDISPLAY Command Results
Increasing the LVM Logical Volume Size
Now that we have increased the size of our Virtual Hard Disk (VHD), we have created a new Logical Partition to store the data on, our last step is to expand the LVM Volume.
First we need to initialize the new Physical Volume for use with LVM by using this command
pvcreate /dev/hda? (? is whatever number you created using the CFDISK tool) or
Next we need to run
This will give you an output similar to the picture above. From this we need to get the VG Name of the volume you need to add the space to, i.e. web2 in my case. To add the new partition to the Volume Group simply run the following command
vgextend web2 /dev/hda? (where WEB2 is your VG Name and ? is the Partition you created earlier)
Next we need to add the new partition space to the Logical Volume. Most people will want to add this to the Root LV but you may also have a Home LV. To do this we run the following command
lvresize -l +100%FREE /dev/web2/root (where /dev/web2/root is the LV Name of the volume you want to increase)
Linux DF -H Command
Online Expansion of Linux File System
Well if you've made it to this step congratulations, this final step involves expanding the Linux File System to make use of the new free space we just added. This can be run safely even while the machine is booted. Simply type the following command to perform the online resize.
resize2fs /dev/web2/root (where /dev/web2/root is your LV Name from the previous section)
Lastly you can run a DF -H command to view your new free space (see the image above)
Thanks for reading my post and as always if you have any issues feel free to leave a comment or contact me through my webpage One IT - IT Services and I'll help you out.