I have a VM managed by Vagrant using VirtualBox on a Ubuntu host server. My Vagrant box uses the Puppetlabs Debian 6.0.7 basebox, which uses LVM for its root partition.

By default the disk is 8GB which is too small for my purposes. I would like to:

  1. Increase the size of the existing disk and the file system on it without destroying and recreating my VM.
  2. Configure Vagrant so that in future it will create a larger disk for this project.

Can anyone explain how to do this?

up vote 23 down vote accepted

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}.

  • 3
    Note that after resizing and before starting up the vagrant box again, you also need to update the VirtualBox storage configuration so it uses the new VDI and not the original VMDK: In VirtualBox > Storage >Remove existing hard disk > Add hard disk (select existing and point to the new VDI image) – Steve Swinsburg Jul 10 '14 at 4:24
  • For Debian users, I suggest also looking at this post: blog.lenss.nl/2012/09/resize-a-vagrant-vmdk-drive. When I got to step 3 it was not quite as simple as running resize2fs. Good luck! – phirschybar Feb 1 '15 at 21:18
  • 1
    I just wrote a consolidated guide on this: medium.com/@phirschybar/… – phirschybar Feb 1 '15 at 21:47

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
  • Hi There, I would like to try your solution out. Can you confirm where in the Vagrant File this section of code was added in? Kind Regards – Rudi Strydom Sep 16 '14 at 11:48
  • @RudiStrydom I've update the answer - hope it makes more sense now. – Chris Snow Sep 16 '14 at 17:38
  • 2
    This should add in the config.vm.provider :virtualbox do |vb| block to make things more clear – B T Oct 5 '14 at 2:17
  • 1
    If you get an "specify either size or extents" error for lvextend, try lvextend -l +100%FREE /dev/VolGroup/lv_root instead – Andomar Oct 8 '14 at 12:19
  • 1
    Your example would create a 5TB disk (the --size unit is MB, see virtualbox.org/manual/ch08.html#vboxmanage-createvdi). – backflip Jan 31 '15 at 10:00

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

You can use the plugin, which will enable you to specify the disk size https://github.com/sprotheroe/vagrant-disksize

disclaimer: answer most likely applicable only for distros using logical volume management like rhel for which the following use case applies:

I have been using the official Red Hat Enterpise 7.2 vagrant box officially provisioned by red hat.

(you will need a rhel developer account which you can create for free)

Upon installation I was troubled by the fact that only 8GB were also available:

sudo df -h
[vagrant@rhel-cdk ~]$ sudo df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-root   8G  2,5G   6,5G  28% /
devtmpfs                     234M     0  234M   0% /dev
tmpfs                        245M     0  245M   0% /dev/shm
tmpfs                        245M  4,3M  241M   2% /run
tmpfs                        245M     0  245M   0% /sys/fs/cgroup
/dev/sda2                    297M  134M  164M  45% /boot
tmpfs                         49M     0   49M   0% /run/user/1000

And as you can see most of the space had already been consumed by the rhel installation.

However I discovered that the volume group corresponding to the /dev/mapper/VolGroup00-root fileystem did have extra space (which I suppose in terms of virtual box was dynamicaly allocated)

sudo vgdisplay VolGroup00
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               39,70 GiB
  PE Size               4,00 MiB
  Total PE              10164
  Alloc PE / Size       7872 / 30,75 GiB
  Free  PE / Size       2292 / 8,95 GiB
  VG UUID               JBVwpl-13KX-HbQw-FqUa-CA9w-swpF-dF6glm

So the only things remaining to be done were to:

a) Increase the size of our logical volume by say 10G

sudo lvextend -L+10G /dev/VolGroup00/root

and

b) Update your root filesystem so that it becomes aware of this change

sudo xfs_growfs /dev/mapper/VolGroup00-root

(pls note that at least for rhel 7.2 resize2fs will not work for the this purpose).

After that, additional space was available to my filesystem:

sudo df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-root   18G  2,5G   16G  14% /
devtmpfs                     234M     0  234M   0% /dev
tmpfs                        245M     0  245M   0% /dev/shm
tmpfs                        245M  4,3M  241M   2% /run
tmpfs                        245M     0  245M   0% /sys/fs/cgroup
/dev/sda2                    297M  134M  164M  45% /boot
tmpfs                         49M     0   49M   0% /run/user/1000

... no intervention on virtualbox level

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.