I'm setting something up and needed to add a bunch of space to an EXT4 partition.

My current layout is this, a 128GB disk (/dev/sdb) and a single partition (/dev/sdb1) formatted with EXT4. It's a simple layout.

My need is to expand that to 500GB. I expanded the disk in Hyper-V to 500GB and, unfortunately, had to reboot the VM to get the system to pick up the new space on the disk. The logs showed that the disk size changed but I could not find a way to get the kernel to see it, very sad.

Once the reboot was completed I used fdisk to resize the partition then resize2fs to resize the filesystem. I did not unmount the filesystem during this process, EXT4 can be resized online.

After doing this a second time and needing to convert to a GPT due to the size I described the process for resizing a GPT disk as well.

Disclaimer

I can not be held liable for any damages or data loss caused by following these instructions. It is your responsibility to maintain proper backups and data retention policies in the case something goes awry.

Assumptions

I am assuming you are doing this on a disk with a single partition. I have some notes in the steps where you need to change the partition number.

Notes

You may need to re-scan your disk structure in order for this work. You can either reboot the VM, which is the easiest route, or hunt down the disk in the /sys/class/scsi_device directory and tell the kernel to re-scan it.

To do this, run ls -l /sys/class/scsi_device/*/device/block. This will give you a result like this:

'/sys/class/scsi_device/0:0:0:0/device/block':
total 0
drwxr-xr-x 12 root root 0 Mar 20 13:23 sda

'/sys/class/scsi_device/0:0:1:0/device/block':
total 0
drwxr-xr-x 10 root root 0 Mar 20 13:23 sdb

'/sys/class/scsi_device/0:0:2:0/device/block':
total 0
drwxr-xr-x 10 root root 0 Mar 20 13:23 sdc

'/sys/class/scsi_device/2:0:0:0/device/block':
total 0
drwxr-xr-x 9 root root 0 Mar 20 13:23 sr0

I am expanding sdb so the path I need to use is /sys/class/scsi_device/0:0:1:0/device/block

Now echo 1 to the correct path, note the difference in the path, it is surrounded by single quotes and replaced block with rescan:

echo 1 | sudo tee '/sys/class/scsi_device/0:0:1:0/device/rescan'

Check to see if it picked up the resized disk by running:

dmesg | tail -n 10

That should output something containing lines similar to this:

[1057915.900799] sd 0:0:1:0: [sdb] 4294967296 512-byte logical blocks: (2.20 TB/2.00 TiB)
[1057915.900884] sdb: detected capacity change from 1099511627776 to 2199023255552

GPT Specific Notes

The 2 applications used in this process is gdisk and partprobe. The gdisk application is in the gdisk apt package. The partprobe is in the parted apt package.

You can make sure both are installed by running

sudo apt install gdisk parted

MBR Specific Notes

MBR partition tables only support up to a 2 terabyte disk. Anything larger than that and you need to convert it to GPT partition table. This guide does not cover that, there are a number of them out there.

Steps

  1. Prepare the VM:
    • Expand the disk in your hypervisor
    • Take a snapshot of the VM or disk you are expanding
  2. Repartition (fdisk - MBR disks only, for GPT see the next step)
    • sudo fdisk /dev/sdb
      • Command (m for help): p <- print the partition layout, take note of the start sector of the last partition. For me it was 2048 and I only had the one partition, so that was 1
      • Command (m for help): d 1 <- delete the last partition, replace 1 with the last partition number if it is not 1
      • Command (m for help): n <- create the new partition
      • Partition Number: 1 <- the number of the partition you just deleted
      • First sector (#-#): 2048 <- the start sector of the last partition. This is in the output of p from above
      • Last sector, +/-sectors or +/-size{K,M,G,T,P} (#-#, default #): <- just press enter here to use the entire disk.
      • Do you want to remove the signature? [Y]es/[N]o: N <- this was not in any guides I could find, since it sounded scary and a bad idea I chose No and it worked
      • Command (m for help): p <- verify the new partition table
      • Command (m for help): w <- write the new partition table
      • Command (m for help): q <- exit fdisk
  3. Repartition (gdisk - GPT disks only)
    • sudo gdisk /dev/sdb
      • Command (? for help): x <- enter expert mode
      • Expert command (? for help): i <- display detailed information about the partition. Take note of the Partition GUID code, Partition unique GUID and First sector.
      • Expert command (? for help): m <- back to the regular menu
      • Command (? for help): b <- backup the partition table
      • Enter backup filename to save: <put a file path here> <- where to save the backup
      • Command (? for help): d 1 <- delete the partition
      • Command (? for help): n <- create a new partition
      • Partition number (1-128, default 1): <- the number of the partition you deleted, I just press enter here
      • First sector (34-4294967262, default = 2048) or {+-}size{KMGTP}: 2048 <- this should be set to the value of the First sector from when you displayed the detailed information. This is crucial, if it is not the same you will corrupt your disk
      • Last sector (2048-4294967262, default = 4294967262) or {+-}size{KMGTP}: <- just press enter here to use the entire disk
      • Hex code or GUID (L to show codes, Enter = 8300): <- put in the Partition GUID code from above.
      • Command (? for help): x
      • Expert command (? for help): c 1 <- change the partition guid
      • Enter the partition's new unique GUID ('R' to randomize): <- put in the Partition unique GUID from above
      • Expert command (? for help): m <- back to the regular menu
      • Command (? for help): w <- write the partition table
      • Do you want to proceed? (Y/N): y <- actually write the table
      • sudo partprobe <- reload the partition table
  4. Expand the partition
    • sudo resize2fs /dev/sdb1
  5. Check the available space
    • df -h
  6. Remove the snapshot

Conclusion

Resizing a disk by deleting the partition table and recreating is scary. Really scary. But having the directions, step-by-step in front of you it should be much easier. And as always, whenever playing with data, take a snapshot of the virtual machine and make sure you have good backups of your data.