ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Expanding the Size of Disk Volume on a Hyper-V Linux Guest.

Updated on August 03, 2011

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
cfdisk /dev/hda
or
cfdisk /dev/sda
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
pvcreate /dev/sda?

Next we need to run
lvdisplay

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.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Mark 5 years ago

      Being relatively new to the Linux world this was an outstanding write-up. Except for one thing, I had to reboot before I could initialize the physical volume...not a big deal and truly made my life much easier. I didn't have to build a new server. Thanks.

    • Oneit profile image
      Author

      One IT Ltd 5 years ago from Auckland

      Hi Mark, glad I was able to help. When you say you had to reboot before you could initialize the physical volume at what stage was this, after increasing the size of the VHD? I don't recall needing to do any initialization.

    • profile image

      Nablus 5 years ago

      What tool you use to create a VHD for linux. I want to move my existing red hat 6.0 enterprise server to hyper v.

    • profile image

      sotheron 5 years ago

      Thanks for the write up. As with Mark I'm also new to the Linux world and needed to expand a disk running on Hyper-V. I tried a few other articles somehow corrupting the disk in the process, (not a huge issue when you have backups!).

      Also as Mark says I had to rboot before the 'pvcreate /dev/hda?' command. I'm running ubuntu, (if it makes a difference).

      Everything seems to be working okay but I did get messages saying 'Found duplicate PV......' so that's the next thing to learn....

      Thanks again.

    • Oneit profile image
      Author

      One IT Ltd 5 years ago from Auckland

      I can't remember needing to reboot before I was able to run the 'pvcreate /dev/hda' command, next time I need to expand I'll make note of if it is needed.

    • profile image

      Cthulhu 4 years ago

      It's not working for me: after expanding I get:

      Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

    • profile image

      Manuel 3 years ago

      THANK YOU! I was looking everywhere for easy to follow instructions. Your post was the best and easiest to follow!

    • profile image

      Cuong Tran 3 years ago

      Thank u very much. I did it

    • profile image

      fyrye 3 years ago

      Oneit, ty for the write up and pictures.

      For the rebooting issues, it depends on the linux distro.

      See: https://access.redhat.com/site/solutions/57542

    • profile image

      mitch 3 years ago

      I'm a Linux noob and this was INCREDIBLY helpful. Thank you very much for your time and effort!

    • profile image

      Luke 11 months ago

      Hi there,

      Thank you very much for the guide. Had been going well up until I had to do the lvdisplay. I type the command, but nothing shows - just goes to the next line in the terminal. Do you have any clues as to what I need to do to get the info required to move forward?

      I am running ubuntu 15.10.

      Thanks very much for your help,

      Luke

    Click to Rate This Article