Expanding the disk size on a ubuntu linux hyper-V guest

Posted on 2013-05-16
Last Modified: 2014-11-12

Please assist me with expanding the size of the physical disk shown in the attachment (which is the output of cfdisk.)  

The server is Ubuntu 11.04.  I have gone through the basic steps for adding hyper-v to the server (hv_vmbus, hv_storvsc, hv_blkvsc, hv_netvsc)  The virtual network is working good.  The only problem is the server not using the full size of my virtual disk, as shown in the attachment.

Any sort of assistance is greatly appreciated.

output of cfdisk on the hyper-v guest
Question by:stephenmiller
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3

Expert Comment

ID: 39175053
An output from fdisk -l /dev/hda would be more useful in this situation.

This is one of those questions I think we all hate to answer, because the answer scares people (and reasonably so).

Unless your running LVM, a partition is a linear block of cylinders on the disk. To "extend" it you have to first clear any blocking partitions from the end of it, delete the partition entry, and create a new partition of larger size starting on the same cylinder.

In your case you're fairly lucky in that it doesn't look like you have an data partitions on /dev/hda except the one you want to extend. Blowing away the swap partition and recreating it is fine.

If you can use a Gparted LiveCD I highly recommend it, as it will make the process a lot easier, but assuming you can't you can go the old fdisk way.

Because your dealing with ext3, technically it is possible to do this live, with the partition mounted, but I would recommend against it if you have any LiveCD alternative.

If you really care about your data, back it up before you begin.

If you're doing this live you'll need to unmount your swap partition before continuing (do all of this as root):
# swapoff /dev/hda5
# umount /dev/hda5

Open in new window

Now get the cylinder information:
# fdisk -l /dev/hda

Open in new window

Note which cylinder /dev/hda1 starts on, you'll need it when recreating the partition. Since you have a bit of free space before it it almost certainly won't be the firs cylinder. You will also want to note how many cylinders the swap partition uses. It looks like you have about 2GB of swap space, but the number of cylinders will be determined by your installation's cylinder unit size.

Now, fire up fdisk -- start by using command 'm' to get further command info. I'm not in front of a computer right now, so I can give you the actual output. You will want t use 'd' to delete the existing partitions. It will ask you to give a number corresponding to the partition to be deleted. Delete both of them.

Now create a new partition starting on the same cylinder as your original /dev/hda1 by using the commands 'n' (new partition), 'p' (primary), '1' (first partition), <the starting cylinder you noted at the beginning>, <the last cylinder to use for the partition>

Remember to leave enough space for the swap partition when you're deciding on that last cylinder. It should be the total number of cylinders minus the swap swap size.

Now, create the swap partition in the same way at the end of the disk. It's also a primary partition -- don't worry, you'll flag it as swap in the next step.

Once both partitions have been created (use the 'p' command to print the table for display) you can set their flags.

To flag the first partition as bootable, use the commands 'a' and '1'.

To flag the  swap partition use 't', '2', '82'. Note that the second option might be a number greater than 2 if you assigned a higher partition number. The '82' represents swap, but I believe you can use the 'L' command at that step if you want a full type code list.

A this point you need to do a final check ('p' command one more time) and if you are satisfied, write the table to disk with 'w'.

If you did this live you'll likely get a warning telling you that the partition table can't be re-read on a live partition, so you'll have to restart before continuing. If you are dong it from a LiveCD then you're ready to continue.

If all has gone well up to this point, the only thing left to do is resize the filesystem to match the partition size. This is the easiest step.
# resize2fs /dev/hda1

Open in new window

Now, it's possible that your swap partition has changed partition number. If so, update /etc/fstab to reflect this -- for example /dev/hda5 may have become /dev/hda2 (use fdisk -l to check). You can also manually reenable swap without restarting using (as root) swapon /dev/hda2.

That's pretty much it ;)

Expert Comment

ID: 39175084
As an additional note, some people would recommend you fsck /dev/hda1, drop the journal (man tune2fs for further details), and e2fsck /dev/hda1 before (and after) performing the resize2fs call, and then rejournal it after the resize is completed.

That's fine advice if you don't have to do it to a live disk, but you can't safely fsck a live partition.

If you have a LiveCD option and can take the partition offline, I highly recommend doing so. If not, schedule a filesystem check for the next reboot.

Expert Comment

ID: 39175114
Reading back over my first response, I realize I had a few typos in there. Again, I'm not in front of a computer at the moment -- and mobile devices tend to apply "auto-correct" both selectively and painfully, heh.

I do apologize about the apparent grammatical errors, but the commands themselves look good.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 39175832
Hi, thanks for the replies.  The server is new and not yet in production, so there is no concern about loss of data.  I can use a Gparted live CD if necessary.  I didn't get very far with the procedure because the first command throws an error.

I also included the output of fdisk -l /dev/hda for your reference.

See the attached file, the free space I'm trying to utilize doesn't show up in that output.

Expert Comment

ID: 39175995
Sorry about that, lately I've been using image files for swap, which do need to be passed as arguments to swapon/swapoff.

Since you only have one swap partition, just go with this to disable "all":
# swapoff -a

Open in new window


Expert Comment

ID: 39176037
From the image you posted it looks like /dev/hda1 does start at the first cylinder, so that makes things simple. You'll just pass '1' as your starting cylinder argument when creating the new partition.

Looks like you'll need to delete /dev/hda2 as well, which appears to be an extended partition being used as swap.

Your last cylinder for /dev/hda1 should probably be '123752' (as you have 124014 cylinders total -- subtracting 262 cylinders for the swap).

Then, when you go to create the swap, just use the remaining space on the disk.

Expert Comment

ID: 39176039
By the way, kinda odd to see a VM with 1TB of storage space but only 2GB of RAM allocated to it ;)

I'm used to seeing hefty web server allocations (like 32GB of RAM and up).

Expert Comment

ID: 39176086
As you mentioned you can use a Gparted LiveCD, it's probably the easiest way to go, as you can point and click your way through moving the partitions around.

You can even tell it to create the swap as 2GB at the end of the disk before resizing hda1. And with it's "Resize" right click option you can bypass the manual deletion and setting of cylinders.

If you have the time to learn, doing it "the hard way" can be a good learning experience, but if you're in an office setting, just pop in Gparted, spend the 5 minutes making the changes, and move on with your day =)

Author Comment

ID: 39182241
Hi, thanks again for the replies.  In GParted when I select to resize /dev/sda1 it states that it is already at the maximum size.  I'm not able to increase it's size from the unallocated space.

Accepted Solution

GhostInTheMacheen earned 500 total points
ID: 39187303
Did you move/delete the trailing partitions before attempting the resize? Remember that the partition must be one continuous block.

Based on your cfdisk output I've been assuming an ext3 filesystem which supports at least a 2TB partition size, even if you are only using a 1K block size. Assuming the more standard 4K block size, you ought to be able to resize up to 16TB (if you had the disk space). If it were ext4 you could go higher still.

Based on the cylinder size it looks like you're only using about 125GB for the partition so far.

Author Comment

ID: 39223254
I decided to make a new partition to use as a data store, then encountered issues with the network connectivity completely stopping.  I decided the better option was to uninstall Hyper-V and use VirtualBox, which allowed for the full use of the partition during the ubuntu install.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question