Skip to content

Instantly share code, notes, and snippets.

@dcai
Forked from christopher-hopper/vm-resize-hard-disk.md
Last active March 8, 2020 12:12
Show Gist options
  • Save dcai/b644608aa010100a68937cd639f3f3f0 to your computer and use it in GitHub Desktop.
Save dcai/b644608aa010100a68937cd639f3f3f0 to your computer and use it in GitHub Desktop.
Resize a Hard Disk for a Virtual Machine provisioned using Vagrant from a Linux base box to run using VirutalBox.

Steps to resize the hard disk

  1. Stop the virtual machine using Vagrant.

     # cd vg
     # vagrant halt
    
  2. Locate the VirtuaBox VM and the HDD attached to its SATA Controller.

     # cd ~/VirtualBox\ VMs/bionic_default_1583666562413_23379
     # VBoxManage showvminfo bionic_default_1583666562413_23379 | grep ".vmdk"
    

    The showvminfo command should show you the location on the file-system of the HDD of type VMDK along with the name of the Controller it is attached to - it will look something like this:

     SATA Controller (0, 0): C:\Users\user.name\VirtualBox VMs\bionic_default_1583666562413_23379\box-disk1.vmdk (UUID: 2f79610e-6c06-46d5-becb-448386ea40ec)
    
  3. clone the VMDK type disk to a VDI type disk so it can be resized.

     # cd ~/VirtualBox\ VMs/bionic_default_1583666562413_23379
     # VBoxManage clonehd "box-disk1.vmdk" "dev.vdi" --format vdi
    
  4. Find out how big the disk is currently, to determine how large to make it when resized. The information will show the current size and the Format variant. If Dynamic Allocation was used to create the disk, the Format variant will be "dynamic default".

     # VBoxManage showhdinfo "dev.vdi"
    
  5. Resize the cloned disk to give it more space. The size argument below is given in Megabytes (1024 Bytes = 1 Megabyte). Because this disk was created using dynamic allocation I'm going to resize it to 1T.

     # VBoxManage modifyhd "dev.vdi" --resize 1024000
    

    NOTE: If the disk was created using dynamic allocation (see previous step) then the physical size of the disk will not need to match its logical size - meaning you can create a very large logical disk that will increase in physical size only as space is used.

    TIP: To convert a Gigabyte value into Megabytes use an online calculator.

  6. Find out the name of the Storage Controller to attach the newly resized disk to.

     # VBoxManage showvminfo bionic_default_1583666562413_23379 | grep "Storage"
    
  7. Attach the newly resized disk to the Storage Controller of the Virtual Machine. In our case we're going to use the same name for the Storage Controller, SATA Controller, as revealed in the step above.

     # VBoxManage storageattach bionic_default_1583666562413_23379 --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium dev.vdi
    
  8. Reboot the Virtual Machine using Vagrant.

     # cd vg
     # vagrant up
    
  9. Open a command-line shell as root on the Virtual Machine via ssh.

     # vagrant ssh
     # sudo growpart --dry-run /dev/sdb 1 # or sudo cfdisk /dev/sda
     # sudo resize2fs /dev/sda1
    
@dcai
Copy link
Author

dcai commented Mar 8, 2020

http://www.beginninglinux.com/home/virtualbox/shrinking-vdi-with-zerofree-on-ubuntu-as-guest-os

sudo init 1
zerofree -v /dev/sda1
VBoxManage modifymedium disk "dev.hdi" --compact

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment