Resizing VirtualBox's disk size [duplicate]

I have a VM (VirtualBox) and its disk partitions are set up like this:

$ df -h 
Filesystem      Size  Used Avail Use% Mounted on
udev            979M     0  979M   0% /dev
tmpfs           200M   24M  177M  12% /run
/dev/sda1       7.8G  6.9G  445M  95% /
tmpfs          1000M  216K 1000M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs          1000M     0 1000M   0% /sys/fs/cgroup
tmpfs           200M   52K  200M   1% /run/user/1000
/dev/sr0         58M   58M     0 100% /media/ldl/VBox_GAs_5.2.0

I used VBoxManage to resize the VM's .vdi file

VBoxManage modifyhd "/Users/sof/VirtualBox VMs/ubuntu14.04_01/ubuntu14.04_01.vdi" --resize 51200

and restarted the VM. Unfortunately this not work.

How do I resize the VM's drive?


EDIT-01

The guest OS is Ubuntu 14.04, and I want to extend its disk. but I don't know how to extend the partitions size.


Solution 1:

There are three separate entities at play.

Initially your disk would have looked like this:

disk layout #1

1. Resize the Disk

By issuing the modifyhd command, you will have extended the disk:

disk layout #2

2. Resize the Partition

Now you need to modify the partition so that it uses the extra space. You can use fdisk to do this.

fdisk /dev/sda
  • Take careful note of the partition's start and length using the print (p) command.
  • Delete the partition (d)
  • Create a new partition (n)
    • Use the same first sector as the old partition (now deleted)
    • Either use the default last sector or give another value - just make sure that the new partition is larger than it was previously
  • Write the changes to disk (w)

You may need to use partprobe to force the kernel to re-read the partition tables. Use lsblk to verify that the partition is now the size you expect it to be.

You have now resized the partition too:

disk layout #2

3. Resize the Filesystem

The last step is to resize the filesystem, which can be done using resize2fs if it is an ext2/3/4 filesystem (it probably is). If you provide no size, then the filesystem will be resized to fill the available space.

resize2fs /dev/sda1

This can be done while the filesystem is mounted.

And at last, the filesystem has been resized too:

disk layout #3

You can verify this using df as before.