Link to home
Start Free TrialLog in
Avatar of stephenmiller
stephenmillerFlag for United States of America

asked on

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

Hello,

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.

User generated image
Avatar of GhostInTheMacheen
GhostInTheMacheen
Flag of United States of America image

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 ;)
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.
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.
Avatar of stephenmiller

ASKER

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.
fdisk.png
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

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.
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).
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 =)
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.
ASKER CERTIFIED SOLUTION
Avatar of GhostInTheMacheen
GhostInTheMacheen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.