How can I increase disk size on a Vagrant VM?

I found this simplest way to resolve this problem:

  • Install this plugin: vagrant plugin install vagrant-disksize

  • Edit the Vagrantfile:

    Vagrant.configure('2') do |config|
      ...
      config.vm.box = 'ubuntu/xenial64'
      config.disksize.size = '50GB'
      ...
    end
    
  • vagrant halt && vagrant up

    • Note: this will not work with vagrant reload

Personally I think it'll be easier to attach an extra virtual HDD and mount it to a proper mount point, for example /opt and move your stuff over using rsync to work around this issue, after all, puppet vagrant boxes are for testing purposes.

Reason why: VBoxManage modifyhd only works with native VDI image. However, vagrant base boxes are basically exported OVF/OVA using VMDK format.

See VirtualBox Docs

The --resize x option (where x is the desired new total space in megabytes) allows you to change the capacity of an existing image; this adjusts the logical size of a virtual disk without affecting the physical size much.[37] This currently works only for VDI and VHD formats, and only for the dynamically allocated variants, and can only be used to expand (not shrink) the capacity.

To increase the capacity of disk for Vagrant Base Box

Steps are

  1. To be able to resize the HDD, you'll have to convert it to VDI first, e.g. VBoxManage clonehd in.vmdk out.vdi --format VDI and then re-attached it (using the GUI is easier).

  2. Resize it using VBoxManage modifyhd box.vdi --resize 15360 which increases the capacity to 15GB.

  3. However this only changes the drive capacity, you will have to expand the file system for the guest afterwards. For example, use resize2fs -p -F DEVICE for ext{3,4}.


I've automated adding the disk in my Vagrantfile:

Vagrant.configure("2") do |config|
    ...
    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"
   ...
end

Where the add_new_disk.sh shell script looks like this:

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

This script is for a centos 6.4 box, but could easily be adapted to ubuntu.

Instead of adding a disk, other options include:

  • using a box with a bigger disk such as opscode bento which have 40Gb disks
  • build your own box using packer. You can use the opscode boxes packer definitions as a starting point