Nur Rony
Polyglot Programmer. Blindly love JS. An automation freak who love to cook code in React, Redux, RxJS and serve them using Docker. Married to the most beautiful girl.

Resize Hard Disk of a Vagrant box

Resize Hard Disk of a Vagrant box

While working with Vagrant, we may need to change Harddisk. But AFIAK, Vagrant does not provide a straight solution to resize the hard disk. I tried to google a lot but most of the solution I found did not work for me. The following solution works for me.

Using vagrant-disksize plugin

This is the easiest way to resize hard disk of a Vagrant box. It is sufficient for most of the use cases.

Install and using vagrant-disksize plugin
Just run the following command in your terminal

vagrant plugin install vagrant-disksize

then update your Vagrantfile as follows

Vagrant.configure('2') do |config| = 'ubuntu/xenial64'
  config.disksize.size = '60GB'

Using VBoxManage

As Vagrant use VBoxManage under the hood, you can do whatever you want to do with your Vagrant box with it

vagrant box add ubuntu/trusty64 --box-version 20150609.0.9
cd ~/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-trusty64/20150609.0.9/virtualbox/
VBoxManage clonehd box-disk1.vmdk tmp-disk.vdi --format vdi
VBoxManage modifyhd tmp-disk.vdi --resize 61440
VBoxManage clonehd tmp-disk.vdi resized-disk.vmdk --format vmdk
rm tmp-disk.vdi box-disk1.vmdk
mv resized-disk.vmdk box-disk1.vmdk

Generally it comes with 40GB base disk and after this the base HDD size should be 60GB.

Manually Updating Vagrantfile

config.vm.provider :virtualbox do |vb|
file_to_disk = File.realpath( "." ).to_s + "/disk.vdi"

    if ARGV[0] == "up" && ! File.exist?(file_to_disk) 
       puts "Creating 5GB disk #{file_to_disk}."
       vb.customize [
            '--filename', file_to_disk, 
            '--format', 'VDI', 
            '--size', 5000 * 1024 # 5 GB
       vb.customize [
            'storageattach', :id, 
            '--storagectl', 'SATA Controller', 
            '--port', 1, '--device', 0, 
            '--type', 'hdd', '--medium', 
 config.vm.provision "shell", path: "scripts/"

The content is as follows

set -e
set -x

if [ -f /etc/disk_added_date ]
   echo "disk already added so exiting."
   exit 0

sudo fdisk -u /dev/sdb <<EOF


pvcreate /dev/sdb1
vgextend VolGroup /dev/sdb1
lvextend /dev/VolGroup/lv_root
resize2fs /dev/VolGroup/lv_root

date > /etc/disk_added_date

Helpful Links: