Skip to content

Instantly share code, notes, and snippets.

@kenji4569
Created January 10, 2020 02:59
Show Gist options
  • Save kenji4569/e4c6de015f57ee3747bc0ba12dabcc5d to your computer and use it in GitHub Desktop.
Save kenji4569/e4c6de015f57ee3747bc0ba12dabcc5d to your computer and use it in GitHub Desktop.

Resize Vagrant Disk

Step 1: Create VDI disk from VMDK disk type

# Turn off your Vagrant machine
$ vagrant halt

# Comment out this line in Vagrantfile
# config.disksize.size = '30GB'

# Go to `~/VirtualBox\ VMs` directory
$ cd ~/VirtualBox\ VMs

# Rename your VirtualBox machine to nearme_ubuntu_1604 (Optional)
$ vboxmanage modifyvm NearMe_default_xxxx --name nearme_ubuntu_1604

# Locate the VM and the HDD attached to its SATA/IDE Controller. In this instance we're assuming the VM is located in the default location
$ cd nearme_ubuntu_1604
$ VBoxManage showvminfo nearme_ubuntu_1604 | grep ".vmdk"

IDE Controller (0, 0): /Users/hnq90/VirtualBox VMs/nearme_ubuntu_1604/packer-ubuntu-16.04-x86_64-virtualbox-vagrant-puppet-1465223024-disk1.vmdk (UUID: 3d05781e-de57-4b15-8f60-7b42b40ddab0)

# If VMDK disk found, clone it to VDI type so can it be resized
$ VBoxManage clonehd "packer-ubuntu-16.04-x86_64-virtualbox-vagrant-puppet-1465223024-disk1.vmdk" "clone-disk.vdi" --format vdi

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vdi'. UUID: c0f8f8a2-7137-402d-877f-6e9fa4974632

# Show how big the disk is currently
$ VBoxManage showhdinfo "clone-disk.vdi"

UUID:           c0f8f8a2-7137-402d-877f-6e9fa4974632
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /Users/hnq90/VirtualBox VMs/nearme_ubuntu_1604/clone-disk.vdi
Storage format: vdi
Format variant: dynamic default
Capacity:       20480 MBytes
Size on disk:   2813 MBytes
Encryption:     disabled

# Resize the cloned disk to give it more space
$ VBoxManage modifyhd "clone-disk.vdi" --resize 30720 # 30 GB

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

# Re-show how big the disk is after resizing
$ VBoxManage showhdinfo "clone-disk.vdi"

UUID:           c0f8f8a2-7137-402d-877f-6e9fa4974632
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /Users/hnq90/VirtualBox VMs/nearme_ubuntu_1604/clone-disk.vdi
Storage format: vdi
Format variant: dynamic default
Capacity:       30720 MBytes # => 20GB to 30GB
Size on disk:   2813 MBytes
Encryption:     disabled

# Find out the name of the Storage Controller to attach the newly resized disk to
$ VBoxManage showvminfo nearme_ubuntu_1604 | grep "Storage"

Storage Controller Name (0):            IDE Controller
Storage Controller Type (0):            PIIX4
Storage Controller Instance Number (0): 0
Storage Controller Max Port Count (0):  2
Storage Controller Port Count (0):      2
Storage Controller Bootable (0):        on

# Attach the newly resized disk to the Storage Controller of the Virtual Machine
$ VBoxManage storageattach nearme_ubuntu_1604 --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium clone-disk.vdi

Step 2: Resize partitions by GParted Live CD

Attach GParted as Live CD

  • Start the nearme_ubuntu_1604 VM in VirtualBox interface.

Start VM

  • Boot into the GParted Live CD by pressing F12 and select CD-ROM from Boot menu.

Choose CD-ROM

Start GParted Live CD

  • Right click on /dev/sda2 and choose Resize/Move. Set Free space following to 0 and click Resize/Move button.

Resize /dev/sda2

  • Click on Apply All Operations to resize /dev/sda2.

Apply operations /dev/sda2

  • Right click on /dev/sda5 and choose Resize/Move. Set Free space following to 0 and click Resize/Move button.

Resize /dev/sda5

  • Click on Apply All Operations to resize /dev/sda5.

Apply operations /dev/sda5

  • Shutdown the VM after above step done.

  • Remove GParted Live CD from Storage Devices.

Step 3: Extend LVM

  • Start the Vagrant machine and login to root user:
$ cd ~/Projects/NearMe/nearme-center # NearMe project path
$ vagrant up
$ sudo su -
# ...
  • Find the logical volume which need to resize:
$ df -h

Filesystem                      Size  Used Avail Use% Mounted on
udev                            2.9G     0  2.9G   0% /dev
tmpfs                           597M  8.2M  589M   2% /run
/dev/mapper/localhost--vg-root   19G  3.0G   15G  17% /         # => Use this file system
tmpfs                           3.0G     0  3.0G   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           3.0G     0  3.0G   0% /sys/fs/cgroup
/dev/sda1                       472M   53M  395M  12% /boot
tmpfs                           597M     0  597M   0% /run/user/1000
vagrant                         233G  207G   27G  89% /vagrant
  • Extend the logical volume to use 100% free space of the Volume Group size.
lvextend -l +100%FREE /dev/mapper/localhost--vg-root
  • Resize the file-system to use up the space made available in the Logical Volume.
resize2fs /dev/mapper/localhost--vg-root
  • Verfiy that there is now more space available.
Filesystem                      Size  Used Avail Use% Mounted on
udev                            2.9G     0  2.9G   0% /dev
tmpfs                           597M  8.2M  589M   2% /run
/dev/mapper/localhost--vg-root   29G  3.0G   25G  12% /
tmpfs                           3.0G     0  3.0G   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           3.0G     0  3.0G   0% /sys/fs/cgroup
/dev/sda1                       472M   53M  395M  12% /boot
tmpfs                           597M     0  597M   0% /run/user/1000
vagrant                         233G  207G   27G  89% /vagrant
  • Restart VM to ensure all the services are running correctly.
$ exit
$ exit
$ vagrant reload
# ...
  • Turn off the Vagrant machine.
$ cd ~/Projects/NearMe/nearme-center # NearMe project path
$ vagrant halt
# ...
  • Remove the VMDK disk when all above steps are done.
$ cd ~/VirtualBox\ VMs/nearme_ubuntu_1604
$ rm -f packer-ubuntu-16.04-x86_64-virtualbox-vagrant-puppet-1465223024-disk1.vmdk
$ ls -la

total 5787680
drwx------  6 hnq90  staff   192B Aug  5 18:33 .
drwx------  4 hnq90  staff   128B Aug  5 17:46 ..
drwx------  6 hnq90  staff   192B Aug  5 18:17 Logs
-rw-------  1 hnq90  staff   2.8G Aug  5 18:33 clone-disk.vdi
-rw-------  1 hnq90  staff   4.6K Aug  5 18:17 nearme_ubuntu_1604.vbox
-rw-------  1 hnq90  staff   4.6K Aug  5 18:17 nearme_ubuntu_1604.vbox-prev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment