[Infrastructure as Code (IaC) - Vagrant] Vagrant add external disk on Oracle VirtualBox

Usage

First, edit Vagrant file to add external disk configuration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true

# Customize the amount of memory on the VM:
vb.memory = "1024"

+ file_to_disk = './disks/external-disk-1.vdi'
+ # 100 * 1024 = 100GB
+ vb.customize ['createhd', '--filename', file_to_disk, '--size', 100 * 1024]
+ # IDE mode
+ vb.customize ['storageattach', :id, '--storagectl', 'IDE', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
+ # SATA Controller mode
+ # vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
end

Then, run vagrant up or vagrant reload to make external disk available.

1
2
3
4
$ VAGRANT_EXPERIMENTAL=disks vagrant up

# Or reload
# $ VAGRANT_EXPERIMENTAL=disks vagrant reload

Done!

1
2
3
4
5
$ vagrant ssh -c lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20.0G 0 disk
├─sda1 8:1 0 19.6G 0 part
sdb 8:16 0 100.0G 0 disk

FAQs

Could not find a controller named ‘SATA Controller’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ VAGRANT_EXPERIMENTAL=disks vagrant up
...
A customization command failed:

["storageattach", :id, "--storagectl", "SATA Controller", "--port", 1, "--device", 0, "--type", "hdd", "--medium", "./disks/disk-1.vdi"]

The following error was experienced:

#<Vagrant::Errors::VBoxManageError: There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["storageattach", "5454cd06-0f61-43c5-a14e-071c9100439c", "--storagectl", "SATA Controller", "--port", "1", "--device", "0", "--type", "hdd", "--medium", "./disks/disk-1.vdi"]

Stderr: VBoxManage: error: Could not find a controller named 'SATA Controller'
>

Please fix this customization and try again.

Check current Storage Controller Name.

1
2
3
4
5
6
7
$ VBoxManage showvminfo 5454cd06-0f61-43c5-a14e-071c9100439c | grep "Storage"
Storage Controller Name (0): IDE
Storage Controller Type (0): PIIX4
Storage Controller Instance Number (0): 0
Storage Controller Max Port Count (0): 2
Storage Controller Port Count (0): 2
Storage Controller Bootable (0): on

Change Storage Controller Name SATA Controller from to IDE.

1
2
3
4
5
+     # IDE mode
+ vb.customize ['storageattach', :id, '--storagectl', 'IDE', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
+ # SATA Controller mode
+ # vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
- vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]

VERR_ALREADY_EXISTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ VAGRANT_EXPERIMENTAL=disks vagrant up
A customization command failed:

["createhd", "--filename", "./disks/disk-1.vdi", "--size", 1048576]

The following error was experienced:

#<Vagrant::Errors::VBoxManageError: There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["createhd", "--filename", "./disks/disk-1.vdi", "--size", "1048576"]

Stderr: 0%...
Progress state: VBOX_E_FILE_ERROR
VBoxManage: error: Failed to create medium
VBoxManage: error: Could not create the medium storage unit '/Users/cloudolife/col-vagrant/disks/disk-1.vdi'.
VBoxManage: error: VDI: cannot create image '/Users/cloudolife/col-vagrant/disks/disk-1.vdi' (VERR_ALREADY_EXISTS)
VBoxManage: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium
VBoxManage: error: Context: "RTEXITCODE handleCreateMedium(HandlerArg*)" at line 450 of file VBoxManageDisk.cpp
>

Please fix this customization and try again.

There is already a disk disk-1.

Check the UUID of the disk disk-1

1
2
3
4
5
$ VBoxManage list hdds
...
UUID: <UUID>
Location: /home/vagrant/vagrants/vagrant-example/disks/disk-1.vdi
...

Delete the disk disk-1.

1
$ vboxmanage closemedium disk <UUID> --delete

Then, run VAGRANT_EXPERIMENTAL=disks vagrant up.

References

[1] Add a second disk to system using vagrant - https://gist.github.com/leifg/4713995

[2] Vagrant virtualbox multiple disks - https://gist.github.com/muresan/e81df5d8aaf8be78b4ecdf787a9dba15

[3] How to define multiple disks inside Vagrant using VirtualBox provider – sleeplessbeastie’s notes - https://sleeplessbeastie.eu/2021/05/10/how-to-define-multiple-disks-inside-vagrant-using-virtualbox-provider/

[4] Fix for VBoxManage: error: Could not find a controller named ‘SATA’ Error | Sharmi Writes Here - https://www.minvolai.com/fix-for-vboxmanage-error-could-not-find-a-controller-named-sata-error/

[5] Vagrant by HashiCorp - https://www.vagrantup.com/

[6] Oracle VM VirtualBox - https://www.virtualbox.org/