geckos
geckos

Reputation: 6299

Vagrant, how to specify the disk size?

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?

Upvotes: 74

Views: 78025

Answers (8)

JonTheNiceGuy
JonTheNiceGuy

Reputation: 641

Based on the answer from W1M0R here are some extra notes.

There are three operations which need to occur;

  1. Ask Vagrant to ask the provider to change the size of the disk image. This is (at least as of 2.4.0) a generally available option for VirtualBox based VMs - add this to your Vagrantfile; config.vm.disk :disk, primary: true, size: "50GB"

  2. Ask the guest OS to resize the partition. And then...

  3. Ask the guest OS to change the size of the filesystem on the partition.

So, clearly, parts 2 and 3 are going to vary by OS. I tend to prefer the images from ubuntu (e.g. ubuntu/jammy64 is my most recent Ubuntu LTS image), debian (e.g. debian/bookworm64 is the most recent release) and almalinux (e.g. almalinux/9 is the most recent release). I have not tested this with any other base images.

As of 2023-12-11 (when I write this answer):

  • ubuntu/jammy64 automatically resizes both the partition and the filesystem. We don't need to do anything further here.
  • debian/bookworm64 does not resize the root (and only) partition. It uses the ext4 filesystem, so you will need to execute growpart /dev/sda 1 to resize the partition and resize2fs /dev/sda1 to change the filesystem size.
  • almalinux/9 does not resize the root partition, which is on the disk along with boot and EFI partitions. It uses the xfs filesystem, so you will need to execute growpart /dev/sda 4 to resize the partition and xfs_growfs / to change the filesystem size.

If you are looking to find this data for another image, create a basic image, adjust the disk size with the above command, and then run the following;

lsblk --output=NAME,FSTYPE $(df / --output=source | tail -n 1)

this will return something like:

NAME FSTYPE
sda4 xfs

Which tells you that your OS is using the disk /dev/sda, the partition ID of 4 and the filesystem type xfs. As I don't really know much about other filesystem types right now (LVM instructions are elsewhere in this question page, I'm sure), I'm not going to give more instructions for those filesystems.


Oh, and if you want to test this the way I did?

Vagrant.configure("2") do |config|
  config.vm.provision "shell", before: :all, inline: "df -h /"

  config.vm.define "almalinux_9" do |config|
    config.vm.box = "almalinux/9"
    config.vm.disk :disk, primary: true, size: "50GB"
    config.vm.provision "shell", inline: "dnf install -y cloud-utils-growpart && growpart /dev/sda 4 && xfs_growfs /"
  end

  config.vm.define "debian_bookworm" do |config|
    config.vm.box = "debian/bookworm64"
    config.vm.disk :disk, primary: true, size: "50GB"
    config.vm.provision "shell", inline: "growpart /dev/sda 1 && resize2fs /dev/sda1"
  end

  config.vm.define "ubuntu_jammy" do |config|
    config.vm.box = "ubuntu/jammy64"
    config.vm.disk :disk, primary: true, size: "50GB"
  end

  config.vm.provision "shell", after: :all, inline: "df -h /"
end

which gives you:

$ vagrant up
Bringing machine 'almalinux_9' up with 'virtualbox' provider...
Bringing machine 'debian_bookworm' up with 'virtualbox' provider...
Bringing machine 'ubuntu_jammy' up with 'virtualbox' provider...
==> almalinux_9: Importing base box 'almalinux/9'...
==> almalinux_9: Matching MAC address for NAT networking...
==> almalinux_9: Checking if box 'almalinux/9' version '9.2.20230513' is up to date...
==> almalinux_9: Setting the name of the VM: resize_almalinux_9_1702255543514_46231
==> almalinux_9: Clearing any previously set network interfaces...
==> almalinux_9: Preparing network interfaces based on configuration...
    almalinux_9: Adapter 1: nat
==> almalinux_9: Forwarding ports...
    almalinux_9: 22 (guest) => 2222 (host) (adapter 1)
==> almalinux_9: Configuring storage mediums...
    almalinux_9: Disk 'vagrant_primary' needs to be resized. Resizing disk...
==> almalinux_9: Booting VM...
==> almalinux_9: Waiting for machine to boot. This may take a few minutes...
    almalinux_9: SSH address: 127.0.0.1:2222
    almalinux_9: SSH username: vagrant
    almalinux_9: SSH auth method: private key
    almalinux_9: 
    almalinux_9: Vagrant insecure key detected. Vagrant will automatically replace
    almalinux_9: this with a newly generated keypair for better security.
    almalinux_9: 
    almalinux_9: Inserting generated public key within guest...
    almalinux_9: Removing insecure key from the guest if it's present...
    almalinux_9: Key inserted! Disconnecting and reconnecting using new SSH key...
==> almalinux_9: Machine booted and ready!
==> almalinux_9: Checking for guest additions in VM...
==> almalinux_9: Mounting shared folders...
    almalinux_9: /vagrant => /home/user/temp/resize
==> almalinux_9: Running provisioner: shell...
    almalinux_9: Running: inline script
    almalinux_9: Filesystem      Size  Used Avail Use% Mounted on
    almalinux_9: /dev/sda4        19G  1.3G   18G   7% /
==> almalinux_9: Running provisioner: shell...
    almalinux_9: Running: inline script
    almalinux_9: AlmaLinux 9 - AppStream                         5.1 MB/s | 7.5 MB     00:01
    almalinux_9: AlmaLinux 9 - BaseOS                            2.4 MB/s | 2.4 MB     00:00
    almalinux_9: AlmaLinux 9 - Extras                             28 kB/s |  17 kB     00:00
    almalinux_9: Dependencies resolved.
    almalinux_9: ================================================================================
    almalinux_9:  Package                    Architecture Version          Repository       Size
    almalinux_9: ================================================================================
    almalinux_9: Installing:
    almalinux_9:  cloud-utils-growpart       x86_64       0.33-1.el9       appstream        34 k
    almalinux_9: 
    almalinux_9: Transaction Summary
    almalinux_9: ================================================================================
    almalinux_9: Install  1 Package
    almalinux_9: 
    almalinux_9: Total download size: 34 k
    almalinux_9: Installed size: 75 k
    almalinux_9: Downloading Packages:
    almalinux_9: cloud-utils-growpart-0.33-1.el9.x86_64.rpm      264 kB/s |  34 kB     00:00
    almalinux_9: --------------------------------------------------------------------------------
    almalinux_9: Total                                            65 kB/s |  34 kB     00:00
    almalinux_9: Running transaction check
    almalinux_9: Transaction check succeeded.
    almalinux_9: Running transaction test
    almalinux_9: Transaction test succeeded.
    almalinux_9: Running transaction
    almalinux_9:   Preparing        :                                                        1/1
    almalinux_9:   Installing       : cloud-utils-growpart-0.33-1.el9.x86_64                 1/1
    almalinux_9:   Running scriptlet: cloud-utils-growpart-0.33-1.el9.x86_64                 1/1
    almalinux_9:   Verifying        : cloud-utils-growpart-0.33-1.el9.x86_64                 1/1
    almalinux_9: 
    almalinux_9: Installed:
    almalinux_9:   cloud-utils-growpart-0.33-1.el9.x86_64
    almalinux_9: 
    almalinux_9: Complete!
    almalinux_9: CHANGED: partition=4 start=1462272 old: size=39495680 end=40957951 new: size=103395295 end=104857566
    almalinux_9: meta-data=/dev/sda4              isize=512    agcount=4, agsize=1234240 blks
    almalinux_9:          =                       sectsz=512   attr=2, projid32bit=1
    almalinux_9:          =                       crc=1        finobt=1, sparse=1, rmapbt=0
    almalinux_9:          =                       reflink=1    bigtime=1 inobtcount=1
    almalinux_9: data     =                       bsize=4096   blocks=4936960, imaxpct=25
    almalinux_9:          =                       sunit=0      swidth=0 blks
    almalinux_9: naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
    almalinux_9: log      =internal log           bsize=4096   blocks=2560, version=2
    almalinux_9:          =                       sectsz=512   sunit=0 blks, lazy-count=1
    almalinux_9: realtime =none                   extsz=4096   blocks=0, rtextents=0
    almalinux_9: data blocks changed from 4936960 to 12924411
==> almalinux_9: Running provisioner: shell...
    almalinux_9: Running: inline script
    almalinux_9: Filesystem      Size  Used Avail Use% Mounted on
    almalinux_9: /dev/sda4        50G  1.5G   48G   4% /
==> debian_bookworm: Importing base box 'debian/bookworm64'...
==> debian_bookworm: Matching MAC address for NAT networking...
==> debian_bookworm: Checking if box 'debian/bookworm64' version '12.20231009.1' is up to date...
==> debian_bookworm: Setting the name of the VM: resize_debian_bookworm_1702255580925_29287
==> debian_bookworm: Fixed port collision for 22 => 2222. Now on port 2200.
==> debian_bookworm: Clearing any previously set network interfaces...
==> debian_bookworm: Preparing network interfaces based on configuration...
    debian_bookworm: Adapter 1: nat
==> debian_bookworm: Forwarding ports...
    debian_bookworm: 22 (guest) => 2200 (host) (adapter 1)
==> debian_bookworm: Configuring storage mediums...
    debian_bookworm: Disk 'vagrant_primary' needs to be resized. Resizing disk...
==> debian_bookworm: Booting VM...
==> debian_bookworm: Waiting for machine to boot. This may take a few minutes...
    debian_bookworm: SSH address: 127.0.0.1:2200
    debian_bookworm: SSH username: vagrant
    debian_bookworm: SSH auth method: private key
    debian_bookworm: 
    debian_bookworm: Vagrant insecure key detected. Vagrant will automatically replace
    debian_bookworm: this with a newly generated keypair for better security.
    debian_bookworm: 
    debian_bookworm: Inserting generated public key within guest...
    debian_bookworm: Removing insecure key from the guest if it's present...
    debian_bookworm: Key inserted! Disconnecting and reconnecting using new SSH key...
==> debian_bookworm: Machine booted and ready!
==> debian_bookworm: Checking for guest additions in VM...
    debian_bookworm: The guest additions on this VM do not match the installed version of
    debian_bookworm: VirtualBox! In most cases this is fine, but in rare cases it can
    debian_bookworm: prevent things such as shared folders from working properly. If you see
    debian_bookworm: shared folder errors, please make sure the guest additions within the
    debian_bookworm: virtual machine match the version of VirtualBox you have installed on
    debian_bookworm: your host and reload your VM.
    debian_bookworm: 
    debian_bookworm: Guest Additions Version: 6.0.0 r127566
    debian_bookworm: VirtualBox Version: 7.0
==> debian_bookworm: Mounting shared folders...
    debian_bookworm: /vagrant => /home/user/temp/resize
==> debian_bookworm: Running provisioner: shell...
    debian_bookworm: Running: inline script
    debian_bookworm: Filesystem      Size  Used Avail Use% Mounted on
    debian_bookworm: /dev/sda1        20G  955M   18G   6% /
==> debian_bookworm: Running provisioner: shell...
    debian_bookworm: Running: inline script
    debian_bookworm: CHANGED: partition=1 start=2048 old: size=41940992 end=41943039 new: size=104855519 end=104857566
    debian_bookworm: resize2fs 1.47.0 (5-Feb-2023)
    debian_bookworm: Filesystem at /dev/sda1 is mounted on /; on-line resizing required
    debian_bookworm: old_desc_blocks = 3, new_desc_blocks = 7
    debian_bookworm: The filesystem on /dev/sda1 is now 13106939 (4k) blocks long.
    debian_bookworm: 
==> debian_bookworm: Running provisioner: shell...
    debian_bookworm: Running: inline script
    debian_bookworm: Filesystem      Size  Used Avail Use% Mounted on
    debian_bookworm: /dev/sda1        50G  955M   46G   2% /
==> ubuntu_jammy: Importing base box 'ubuntu/jammy64'...
==> ubuntu_jammy: Matching MAC address for NAT networking...
==> ubuntu_jammy: Checking if box 'ubuntu/jammy64' version '20230524.0.0' is up to date...
==> ubuntu_jammy: Setting the name of the VM: resize_ubuntu_jammy_1702255615996_70261
==> ubuntu_jammy: Fixed port collision for 22 => 2222. Now on port 2201.
==> ubuntu_jammy: Clearing any previously set network interfaces...
==> ubuntu_jammy: Preparing network interfaces based on configuration...
    ubuntu_jammy: Adapter 1: nat
==> ubuntu_jammy: Forwarding ports...
    ubuntu_jammy: 22 (guest) => 2201 (host) (adapter 1)
==> ubuntu_jammy: Configuring storage mediums...
    ubuntu_jammy: Disk 'vagrant_primary' needs to be resized. Resizing disk...
==> ubuntu_jammy: Running 'pre-boot' VM customizations...
==> ubuntu_jammy: Booting VM...
==> ubuntu_jammy: Waiting for machine to boot. This may take a few minutes...
    ubuntu_jammy: SSH address: 127.0.0.1:2201
    ubuntu_jammy: SSH username: vagrant
    ubuntu_jammy: SSH auth method: private key
    ubuntu_jammy: 
    ubuntu_jammy: Vagrant insecure key detected. Vagrant will automatically replace
    ubuntu_jammy: this with a newly generated keypair for better security.
    ubuntu_jammy: 
    ubuntu_jammy: Inserting generated public key within guest...
    ubuntu_jammy: Removing insecure key from the guest if it's present...
    ubuntu_jammy: Key inserted! Disconnecting and reconnecting using new SSH key...
==> ubuntu_jammy: Machine booted and ready!
==> ubuntu_jammy: Checking for guest additions in VM...
    ubuntu_jammy: The guest additions on this VM do not match the installed version of
    ubuntu_jammy: VirtualBox! In most cases this is fine, but in rare cases it can
    ubuntu_jammy: prevent things such as shared folders from working properly. If you see
    ubuntu_jammy: shared folder errors, please make sure the guest additions within the
    ubuntu_jammy: virtual machine match the version of VirtualBox you have installed on
    ubuntu_jammy: your host and reload your VM.
    ubuntu_jammy: 
    ubuntu_jammy: Guest Additions Version: 6.0.0 r127566
    ubuntu_jammy: VirtualBox Version: 7.0
==> ubuntu_jammy: Mounting shared folders...
    ubuntu_jammy: /vagrant => /home/user/temp/resize
==> ubuntu_jammy: Running provisioner: shell...
    ubuntu_jammy: Running: inline script
    ubuntu_jammy: Filesystem      Size  Used Avail Use% Mounted on
    ubuntu_jammy: /dev/sda1        49G  1.5G   47G   3% /
==> ubuntu_jammy: Running provisioner: shell...
    ubuntu_jammy: Running: inline script
    ubuntu_jammy: Filesystem      Size  Used Avail Use% Mounted on
    ubuntu_jammy: /dev/sda1        49G  1.5G   47G   3% /

==> debian_bookworm: Machine 'debian_bookworm' has a post `vagrant up` message. This is a message
==> debian_bookworm: from the creator of the Vagrantfile, and not from Vagrant itself:
==> debian_bookworm: 
==> debian_bookworm: Vanilla Debian box. See https://app.vagrantup.com/debian for help and bug reports

Upvotes: 4

W1M0R
W1M0R

Reputation: 3626

UPDATE 1: Since Vagrant v2.4.0, this is no longer marked as experimental.

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% /

Upvotes: 14

user8234870
user8234870

Reputation:

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 

                                      

Upvotes: 2

JFL
JFL

Reputation: 1522

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% /

Upvotes: 8

codrcodz
codrcodz

Reputation: 21

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.

Upvotes: 2

Firze
Firze

Reputation: 4049

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.

Upvotes: 31

Laurence Chen
Laurence Chen

Reputation: 1848

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

Upvotes: 138

Mahesh Chandra
Mahesh Chandra

Reputation: 65

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

Upvotes: 3

Related Questions