Skip to main content

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|
config.vm.box = 'ubuntu/xenial64'
config.disksize.size = '60GB'
end

Using vagrant-disksize plugin

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 [
'createhd',
'--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',
file_to_disk
]
...
config.vm.provision "shell", path: "scripts/add_new_disk.sh"

The add_new_disk.sh content is as follows

set -e

set -x

if [ -f /etc/disk_added_date ]
then
echo “disk already added so exiting.”
exit 0
fi

sudo fdisk -u /dev/sdb <<EOF
n
p
1


t
8e
w
EOF

pvcreate /dev/sdb1
vgextend VolGroup /dev/sdb1
lvextend /dev/VolGroup/lv_root
resize2fs /dev/VolGroup/lv_root
date > /etc/disk_added_date