Skip to main content

Using Modern Disk Configuration

1. Enabling Disks Feature

The Vagrant disks feature allows you to configure additional storage volumes for your virtual machines. This is particularly useful when you need more disk space than what's provided by the default VM configuration.

Configuration Option Description Default Value Notes
config.vm.disk Enables and configures additional disks for the VM. None Must be called within the Vagrantfile to enable disk configuration.
config.vm.disk.size Specifies the size of the disk in MB or GB. 1024 Can use suffixes like 'GB' or 'MB'. Example: '20GB'
config.vm.disk.mount Determines whether to mount the disk automatically. true If false, manual mounting is required.
config.vm.disk.path Specifies the path where the disk will be mounted. /mnt/disk Can be any valid directory path inside the VM.

Example: Basic Disk Configuration

# Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  # Enable a disk of 20GB
  config.vm.disk size: "20GB", mount: true, path: "/mnt/data"
end
Note: The disks feature is only supported on certain providers such as VirtualBox and VMware. Ensure your provider supports this feature before using it.

2. Creating Primary Disk

Property Value Description
primary_disk.size 10GB Size of the primary disk in gigabytes
primary_disk.type qcow2 Disk image format (e.g., qcow2, vmdk)
primary_disk.path /var/lib/vagrant/disks/primary.img File path where the disk image is stored

Example: Define Primary Disk in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  config.vm.provider "libvirt" do |disk|
    disk.primary_disk.size = "10GB"
    disk.primary_disk.type = "qcow2"
    disk.primary_disk.path = "/var/lib/vagrant/disks/primary.img"
  end
end
Note: The primary disk is created during the VM initialization process and will be used as the root filesystem for the guest OS.

3. Attaching Secondary Disks

Option Values Description
secondary_disk.size 10G Size of the secondary disk to attach
secondary_disk.mount_point /mnt/secondary Mount point for the secondary disk
secondary_disk.format ext4 Filesystem type to format the disk with

Example: Configuring Secondary Disk in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  # Attach secondary disk
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["createhd", "--filename", "secondary.vdi", "--size", "10240"]
    vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--device", "0", "--type", "hdd", "--medium", "secondary.vdi"]
  end

  config.vm.provision "shell", inline: <<-SHELL
    mkdir -p /mnt/secondary
    mkfs.ext4 /dev/sdb
    mount /dev/sdb /mnt/secondary
  SHELL
end

4. Setting Disk Size

Property Value Description
disk.size 10GB Configures the size of the virtual disk in gigabytes
disk.type vdi Specifies the disk format (vdi, vmdk, etc.)

Example: Configuring Disk Size in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  # Set disk size to 20GB
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", 1024]
    vb.customize ["storagectl", :id, "--name", "SATA", "--add", "sata"]
    vb.customize ["createhd", "--filename", "disk.vdi", "--size", 20480]
  end
end
Note: The disk size is specified in megabytes when using the createhd command.
Warning: Modifying disk size after initial creation may require manual steps to expand filesystems.

5. Configuring Disk Name

Property Values Description
disk.name string Name of the disk configuration to be used in Vagrant.
disk.path string Path where the disk image will be created or referenced.
disk.size string Size of the disk (e.g., "10G", "50GB").
disk.type string Type of disk (e.g., "vdi", "vmdk", "qcow2").

Example: Setting Disk Name in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["createhd", "--filename", "my_disk.vdi", "--size", 10240]
    vb.customize ["storageattach", :id, "--storagectl", "SATA", "--port", 1, "--device", 0, "--type", "hdd", "--medium", "my_disk.vdi"]
  end
end

6. Using Provider-Specific Disk Options

Provider Configuration Option Description Example
VirtualBox vm.provider "virtualbox" Configures VirtualBox-specific disk options
vagrant<span>.</span>provider "virtualbox" do |vb|
  vb.customize ["setextrahdd", "disk1.vdi", "1024"]
end
VMware vm.provider "vmware" Configures VMware-specific disk options
vagrant<span>.</span>provider "vmware" do |vm|
  vm.vmx["scsi0:1.present"] = "TRUE"
  vm.vmx["scsi0:1.fileName"] = "disk.vmdk"
end
AWS vm.provider "aws" Configures AWS-specific disk options
vagrant<span>.</span>provider "aws" do |aws|
  aws.root_volume_size = <span>20</span>
  aws.root_volume_type = "gp3"
end
Azure vm.provider "azure" Configures Azure-specific disk options
vagrant<span>.</span>provider "azure" do |azure|
  azure.os_disk_size_gb = <span>30</span>
  azure.storage_account_type = "Premium_LRS"
end
Note: Provider-specific disk options are highly dependent on the underlying virtualization or cloud platform. Always refer to the official documentation for each provider to ensure compatibility and correct syntax.

7. Managing Disk Types

Type Description Usage Notes
disk_type Specifies the type of disk to use "standard", "ssd", "nvme" Used in Vagrant configuration for disk provisioning
disk_size Size of the disk in MB or GB "10G", "512MB" Must be specified when creating a new disk
disk_path Path where the disk is mounted /var/lib/vagrant/disk.img Defaults to /vagrant if not specified

Example: Configuring Disk Type

Vagrant.configure("2") do |config|
  config.vm.disk :disk_type => "ssd", :size => "20G"
end
Note: Disk types are managed by the underlying hypervisor (e.g., VirtualBox, VMware). Ensure your provider supports the specified disk type.

8. Configuring Disk Controller Type

Controller Type Description Notes
ide IDE controller for legacy compatibility Default for older VMs, limited performance
sata SATA controller for modern VMs Recommended for most use cases, better performance
scsi SCSI controller for high-performance needs Supports multiple disks, better I/O performance
nvme NVMe controller for high-speed SSD access NEW in Vagrant 2.3+, requires compatible hypervisor

Example: Setting SATA Controller

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  config.vm.provider "virtualbox" do |vb|
    vb.disk_controller = "sata"
  end
end
Note: The disk controller type affects how disks are attached to the VM and can impact performance and compatibility.

9. Handling Disk Resize on Existing VMs

Step Action Description
1 vagrant halt Shut down the VM to safely perform disk operations.
2 vagrant package Package the current VM state into a box for backup or migration.
3 vagrant box add Add the packaged box to the local Vagrant environment.
4 vagrant up Start the VM with updated disk configuration.

Example: Resizing Disk in VirtualBox

vagrant halt
vboxmanage modifyhd "path/to/disk.vdi" --resize 20480
vagrant up
Note: Ensure that the new disk size is supported by the guest OS and filesystem.
Warning: Resizing disks on running VMs can lead to data corruption. Always back up critical data before resizing.

10. Understanding Disk Feature Limitations

Feature Description Limitations Notes
Disk I/O Performance How fast data can be read/written to the disk Virtualized disks may have lower I/O performance than physical disks Can be mitigated with SSDs or better hypervisor configurations
Disk Space Allocation How disk space is allocated and managed Vagrant may not fully utilize available disk space in some configurations Some providers limit disk size to 10GB by default unless explicitly configured
Snapshot Support Ability to create and restore disk snapshots Not all Vagrant providers support snapshots or have limited snapshot capabilities Snapshotting is provider-dependent (e.g., VirtualBox supports snapshots, while some cloud providers do not)
Disk Encryption Support for encrypted disk storage Some providers or configurations may not support encryption at rest Encryption must be enabled at the provider level or via external tools
Disk Mounting Ability to mount external disks or volumes Mounting external disks is not natively supported in all Vagrant environments Requires provider-specific configuration or manual setup via shell scripts
Note: Disk feature limitations can vary significantly depending on the underlying virtualization provider (e.g., VirtualBox, VMware, Hyper-V) and the host operating system. Always refer to the provider documentation for specific capabilities and constraints.

Was this article helpful?