66

I want to make sure that my development environment has enough free space to install tools and other stuff. I can't find any configuration option about telling to Vagrant the minimum disk size that I want. Is this possible or I need to create my own box?

geckos
  • 5,687
  • 1
  • 41
  • 53
  • 1
    Vagrant has added support for custom disk size (including disk resizing). You can see my answer here for details: https://stackoverflow.com/a/71848893/755405 – W1M0R Apr 13 '22 at 07:30

7 Answers7

127

I have used the vagrant plugin vagrant-disksize to resize the disk.

It worked. It can also help to specify the initial disk size.

Run the following at the command line:

vagrant plugin install vagrant-disksize

and use the following in your Vagrantfile:

vagrant.configure('2') do |config|
    config.vm.box = 'ubuntu/xenial64'
    config.disksize.size = '50GB'
end
Greg Lever
  • 863
  • 12
  • 21
Laurence Chen
  • 1,738
  • 1
  • 13
  • 15
  • Wow, that's should fix everything. I'm not using it right now but I will give it a try. Thanks! – geckos Aug 03 '18 at 21:38
  • What is the file size of the content that Vagrant downloaded if I start it with `vagrant up`? – RoCkDevstack Oct 18 '18 at 04:20
  • 8
    After using this plugin, I destroyed the box and and then did `vagrant up` to get the larger disk to work. `vagrant halt;vagrant up` did not work because the guest OS did not see the new disk space. Rather than fix that new problem, re-creating the box was the easier solution for me and it worked. – Buttle Butkus Feb 22 '19 at 09:16
  • You can also boot your guest machine with a gparted live cd https://gparted.org/download.php and then resize the disk without having to destroy the old the disk. I guess it all depends on which is the greater difficulty in your specific situation. – Lance Dec 27 '19 at 07:56
  • Note that this expands the disk, not the logical volume or the partition. Those have to be expanded in a way dependent on the box you're using. See here for a ubuntu example: https://github.com/sprotheroe/vagrant-disksize/issues/37#issuecomment-573349769 – Chris L. Barnes Apr 08 '20 at 11:19
  • This solution not works in osx Monterey. It does not resize the hdd. – aironman Nov 30 '21 at 16:33
  • 1
    @geckos Vagrant has added support for custom disk size (including disk resizing). You can see my answer here for details: https://stackoverflow.com/a/71848893/755405 – W1M0R Apr 13 '22 at 07:31
29

Install Vagrant plugin vagrant-disksize

vagrant plugin install vagrant-disksize

If you want to make sure user has the plugin installed, when starting vagrant, you can add this in the beginning of Vagrantfile

# Install vagrant-disksize to allow resizing the vagrant box disk.
unless Vagrant.has_plugin?("vagrant-disksize")
    raise  Vagrant::Errors::VagrantError.new, "vagrant-disksize plugin is missing. Please install it using 'vagrant plugin install vagrant-disksize' and rerun 'vagrant up'"
end

Set desired disk size in Vagrantfile

vagrant.configure('2') do |config|
    config.disksize.size = '50GB'
end

Updating existing vagrant box

  1. Do all of the above
  2. Run vagrant halt & vagrant up (You should see something like "Resized disk: old 32768 MB, req 51200 MB, new 51200 MB")
  3. SSH to vagrant box
  4. Run sudo cfdisk /dev/sda
  5. Use arrows to select your disk probably sdaX. Mine was sda3.
  6. Then select resize using arrow keys. Accept the suggested disk size.
  7. Then select write. And answer yes.
  8. You can select quit now.
  9. Run sudo resize2fs -p -F /dev/sdaX You should see something like: "Filesystem at /dev/sda3 is mounted on /; on-line resizing required old_desc_blocks = 4, new_desc_blocks = 6 The filesystem on /dev/sda3 is now 11933952 (4k) blocks long. "
  10. Run df and see that your disk size has increased.
Firze
  • 3,939
  • 6
  • 48
  • 61
  • 1
    I tried to use this method but 'resize' wasn't an option in cfdisk. What do I do from here? – linksassin Mar 03 '20 at 23:30
  • 3
    Step 9 `sudo resize2fs -p -F /dev/sda1` gives me error `resize2fs: Bad magic number in super-block while trying to open /dev/sda1`. `Couldn't find valid filesystem superblock`. Can you please help. – Damodar Bashyal Apr 21 '20 at 03:00
  • What version of vagrant are you using for this? I get errors about vagrant requiring Virtualbox but installing vbox 6.1 fails. I can't find the combination that works – KevinDTimm Aug 04 '20 at 18:11
  • @KevinDTimm I am using VirtualBox 6.1.x and Vagrant 2.2.x. Anyways your problem probably has nothing to do with versions, if you can't get VirtualBox installed. You should figure out what is blocking VirtualBox installation. – Firze Aug 05 '20 at 07:24
  • @Firze - I can't ever get the module vboxdrv installed and so can't complete. Figured out another way so I'm moving on. Thanks for the reply. – KevinDTimm Aug 06 '20 at 12:28
  • @DamodarBashyal: `sudo xfs_growfs /dev/sdaX` as described in an answer below, is the valid command for an XFS filesystem – marc.guenther May 27 '22 at 09:40
  • Between 5 and 6, if the disk you want to grow and free space are not together, you'll need to reorder the partitions with `sfdisk`. See [this answer](https://unix.stackexchange.com/a/90224/429939). – Sadman Sakib Oct 21 '22 at 19:35
9

Vagrant has recently added experimental support for custom disk sizes (including disk resizing) with the VirtualBox provider. Some common usage scenarios are documented here and here.

To activate this feature, modify your Vagrantfile (see the example below), set the VAGRANT_EXPERIMENTAL="disks" environment variable in your shell and then run vagrant up.

Here is an example Vagrantfile tested on Vagrant 2.2.19, using the bento/ubuntu-20.04 base box (which is based on LVM):


Vagrant.configure("2") do |config|
  ...
  config.vm.disk :disk, size: "150GB", primary: true
  config.vm.box = "bento/ubuntu-20.04"
  config.vm.box_version = "202005.21.0"
  config.vm.box_check_update = false
  ...
end

You should see the following output (more or less) during vagrant up:

==> vagrant: You have requested to enabled the experimental flag with the following features:
==> vagrant:
==> vagrant: Features:  disks
==> vagrant:
==> vagrant: Please use with caution, as some of the features may not be fully
==> vagrant: functional yet.
Bringing machine 'server' up with 'virtualbox' provider...
==> server: Clearing any previously set forwarded ports...
==> server: Clearing any previously set network interfaces...
==> server: Preparing network interfaces based on configuration...
    server: Adapter 1: nat
==> server: Forwarding ports...
    server: 22 (guest) => 49222 (host) (adapter 1)
    server: 4200 (guest) => 49280 (host) (adapter 1)
    server: 4243 (guest) => 49243 (host) (adapter 1)
==> server: Configuring storage mediums...
    server: Disk 'vagrant_primary' needs to be resized. Resizing disk...

This process can take a while to complete (in my case, between 1 and 2 hours).

Use vagrant ssh to enter your virtual machine, and then use the following commands (based on this answer) to gather information about your filesystem and then to expand the LVM volume. If your base box does not use LVM, then you shouldn't use these commands.

# Understand your partitions.
> lsblk
> df -lhT /
> sudo fdisk -l /dev/sda
> sudo vgdisplay
> sudo parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start   End     Size    Type      File system  Flags
1      1049kB  538MB   537MB   primary   fat32        boot
2      539MB   68.7GB  68.2GB  extended
5      539MB   68.7GB  68.2GB  logical                lvm

# Resize your partitions.
> sudo parted /dev/sda resizepart 2 100%
> sudo parted /dev/sda resizepart 5 100%
> sudo parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start   End    Size   Type      File system  Flags
1      1049kB  538MB  537MB  primary   fat32        boot
2      539MB   161GB  161GB  extended
5      539MB   161GB  161GB  logical                lvm

# Resize your LVM physical volume to match the new partition extents.
> sudo pvresize /dev/sda5
> sudo vgdisplay
Alloc PE / Size       16255 / <63.50 GiB
Free  PE / Size       22016 / 86.00 GiB

# Resize your LVM logical volume to fill the available Free space.
> sudo lvdisplay
--- Logical volume ---
LV Path                /dev/vgvagrant/root
> sudo lvextend -l+100%FREE /dev/vgvagrant/root
Size of logical volume vgvagrant/root changed from <62.54 GiB (16010 extents) to <148.54 GiB (38026 extents).
Logical volume vgvagrant/root successfully resized.

# Apply the changes to the filesystem.
> sudo resize2fs /dev/vgvagrant/root
> df -lhT /
Filesystem                 Type  Size  Used Avail Use% Mounted on
/dev/mapper/vgvagrant-root ext4  146G   55G   85G  40% /
W1M0R
  • 3,367
  • 3
  • 30
  • 32
7

If your vagrant box uses LVM and is already created, additional steps are required compared to the answer of Firze:

  • Install Vagrant plugin vagrant-disksize
vagrant plugin install vagrant-disksize
  • Set desired disk size in Vagrantfile
vagrant.configure('2') do |config|
    config.disksize.size = '300GB'
end
  • Reboot the vagrant box (You should see something like "Resized disk...")
vagrant halt
vagrant up
  • SSH to the vagrant box

  • Check the old size with vgdisplay:

vgdisplay
  --- Volume group ---
  VG Name               vagrant-vg
  System ID             
  Format                lvm2
  ...
  Alloc PE / Size       16200 / 63.28 GiB
  Free  PE / Size       0 / 0  
  • Assuming that the device is /dev/sda you have to resize the extended and logic partition:
parted /dev/sda
p
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  768MB   767MB   primary   ext2         boot
 2      769MB   68.7GB  67.9GB  extended
 5      769MB   68.7GB  67.9GB  logical                lvm

resizepart 2 321GB
resizepart 5 321GB
p
Number  Start   End    Size   Type      File system  Flags
 1      1049kB  768MB  767MB  primary   ext2         boot
 2      769MB   322GB  321GB  extended
 5      769MB   321GB  320GB  logical                lvm
quit
  • Then, LVM should see the modification:
pvresize /dev/sda5

vgdisplay
  Alloc PE / Size       16200 / 63.28 GiB
  Free  PE / Size       60148 / 234.95 GiB
  • Extend the LVM group:
lvextend  -L+234.95 /dev/vagrant-vg/root 
  Size of logical volume vagrant-vg/root changed from 62.32 GiB (15955 extents) to 297.27 GiB (76102 extents).
  Logical volume root successfully resized.

vgdisplay
  Alloc PE / Size       76347 / 298.23 GiB
  Free  PE / Size       1 / 4.00 MiB
  • Finally, resize the filesystem:
resize2fs /dev/vagrant-vg/root

df -h
/dev/mapper/vagrant--vg-root  293G   59G  222G  21% /
Martin Tapp
  • 3,106
  • 3
  • 32
  • 39
JFL
  • 1,502
  • 14
  • 18
3

This should be done using the provider, i.e the virtualbox configuration settings example would be

https://www.vagrantup.com/docs/virtualbox/configuration.html

config.vm.provider "virtualbox" do |v| v.customize ["modifyvm", :id, "--cpuexecutioncap", "50"] end

https://www.virtualbox.org/manual/ch08.html#vboxmanage-list

but in modifyvm section i dont see anything relating to the disk resize !!maybe you can try and attach a secondary disk !!

https://gist.github.com/leifg/4713995

  • 4
    Is a shame that vagrant hasn't a portable way of doing this. Doing by provider is locking the vagrantfile to that provider.. anyway thanks!!! – geckos Apr 16 '18 at 10:45
2

After installing the vagrant-disksize plugin, like several other answered have mentioned, if you are unable to resize the partition using resize2fs, this is likely because it only supports ext*-formatted filesystem types.

You can check the filesystem type by running mount and finding your device listed there. Most likely, if the device is not using an ext4 filesystem, it is using xfs. If it is, use: sudo xfs_growfs /dev/sdaX to resize the filesystem on the device, where "X" is the device partition you would like to resize.

codrcodz
  • 21
  • 1
1

I had to download the install the vagrant-disksize plugin from the rubygems.org

Installing the vagrant-disksize plugin

$ vagrant plugin install --plugin-clean-sources --plugin-source https://rubygems.org vagrant-disksize

Configuring the disk size in vagrantfile

Vagrant.configure("2") do |config|            
  config.vm.box = "centos/8"                  
  config.vm.network "public_network"          
  config.disksize.size = "11240MB"            
end 

                                      
Udesh
  • 2,415
  • 2
  • 22
  • 32