Resizing GPT Partition on VMware virtual server without loosing data

Jun 25th, 2016 | By | Category: Linux / Freebsd

So you want to enlarge one of your partitions on one of your virtual servers eh?  First things first, shut the instance down and then go into Edit Settings for the instance and change the hard drive to the new size.

Now start up the box and go grab gdisk.  fdisk won’t work well, parted feels like a rickety old boat for what we need so we need the GPT clone of fdisk, aka gdisk

# apt-get install gdisk

Now what you need to do is unmount the partition

# umount /dev/sdb1 (replace sdb1 with whatever your partition is)

Before going any further you have to make extra sure that the partition you are resizing is the last partition on the device.. So if this is partition 2 out of 4 then it’s not going to work, you have to have a continuous runway of new space that starts at the end of the existing partition that you want to resize.  If you aren’t resizing the last partition, or only partition on the device then stop now and go read another tutorial on how to handle that.

The other thing to look out for is if you have a swap partition on this device.. If so you’ll have to turn the swap off so it doesnt’ interfere.

 

Then run gdisk on the partition

# gdisk

Type in your device name, /dev/sdb in my case

You should see this:

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present

Found valid GPT with protective MBR; using GPT.

Bring up some info on the device for good measure by pressing ‘i’

It should show you something like this:

Using 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: AA64B890-306E-497F-8E4D-DFF6A27A9C8E
First sector: 2048 (at 1024.0 KiB)
Last sector: 419428351 (at 200.0 GiB)
Partition size: 419426304 sectors (200.0 GiB)
Attribute flags: 0000000000000000
Partition name: ”

You can also run ‘print’ or ‘p’ for more info

Disk /dev/sdb: 734003200 sectors, 350.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 70116719-27FC-477D-AADB-75E4854CE3B6
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 734003166
Partitions will be aligned on 2048-sector boundaries
Total free space is 314576829 sectors (150.0 GiB)

Number Start (sector)      End (sector)      Size             Code    Name
1               2048                 419428351        200.0 GiB     8300

The most important thing you are going to do is to make note of the First Sector, in my case above it was 2048.  

Next you are going to do something scary and delete the partition.. Don’t worry this isn’t formatting it or deleting any data, it is just changing the partition table.

So press ‘d’ and hit enter

and now when press ‘p’ you should notice that there is no partition visible.  No problem, let’s make a new one

type ‘n’ and hit enter

It is going to ask you for your partition #, seeing as this was my only partition on this drive I hit 1, but if you have 2 then you’ll want to hit 2 or 3 or whatever.

Next thing it will ask you is the First Sector, again crazy important that you put the same value as you read above. In my case it was 2048

First sector (34-734003166, default = 2048) or {+-}size{KMGTP}: 2048

You can see that it defaults to 2048 for me but I still typed it out just to be safe

Next it will ask you for the last sector, for this I just hit enter for the default as it looked to be the end of the new drive size:

Last sector (2048-734003166, default = 734003166) or {+-}size{KMGTP}:

Finally it saked me for the Hex Code of the File system, and defaulted to what it was before(8300) so I entered that.  Likely very very very important that you use the same file system as previously setup or else you’re going to run into problems.. So when you run the print command above make extra sure to pay attention to the Code value.  In my case the partition didn’t have a Name before, it just had the code, but after this process it got a name applied (Linux Filesystem).. This is fine

Hit W and you should see the following:

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.

After this I rebooted the instance and if you run df you will notice the size of the partition is exactly the same as before, womp womp.  Lucky for us there’s an easy command that handles that last step of the process

# resize2fs /dev/sdb1

Which for me outputted the following:

Filesystem at /dev/sdb1 is mounted on /media/mechanical; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 22
The filesystem on /dev/sdb1 is now 91750139 blocks long.

and viola, another run of df will show your drive in it’s new size and glory.. Huzzah!

 

Thanks to this page for the help: https://www.jethrocarr.com/2012/08/10/gdisk-oh-glorious-gdisk/

Tags: , , , , ,

One Comment to “Resizing GPT Partition on VMware virtual server without loosing data”

  1. tecci says:

    So a rookie as me, a convenient software with such utility is important, and what I’m using is AOMEI Partiiton Assistant. But anyway, I have learned a lot from this article, thanks.

Leave a Comment