Conditionally add or remove storage controllers in Vagrant file.

2,573 views
Skip to first unread message

Christopher

unread,
Feb 1, 2016, 5:43:36 PM2/1/16
to Vagrant
Hi all,

I'm looking to make my vagrant file idempotent and almost have it.  However, when I run vagrant up --provision on a machine that has already been created, I get an error about my SATA storage controller.  To avoid this, I have to manually delete the controller in Virtualbox and run the command again.  Removing my SATA controller is ok because it is only used to host ISO files that I am accessing directly to install some software.

Is this even possible?  I'd like to make my provision a little smarter in case I have to force provision an already created machine.  Here's my provider config below:

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 = 4096
   
   
# Customize the amount of CPUs the VM will use:
    vb
.cpus = 2
    vb
.customize ["modifyvm", :id, "--vram", "128"]
    vb
.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
   
# Customize additional Virtualbox settings
    vb
.customize ["sharedfolder", "add", :id, "--name", "VM_Share", "--hostpath", "path/to/share/folder/", "--automount"]


   
#below commented SATA customize will only work when the VM has already been created...on a fresh run this will error since the SATA controller does not exist
   
#vb.customize ["storagectl", :id, "--name", "SATA", "--remove" ]
    vb
.customize ["storagectl", :id, "--name", "SATA", "--add", "sata" ]


   
# Add an ISO file here
    vb
.customize ["storageattach", :id, "--storagectl", "SATA", "--port", "0", "--device", "0", "--type", "dvddrive", "--medium", "path/to/file.iso"]


   
# Add another ISO file here
    vb
.customize ["storageattach", :id, "--storagectl", "SATA", "--port", "1", "--device", "0", "--type", "dvddrive", "--medium", "path/to/file.iso"]
 
end

Thanks in advance,

Christopher

Alvaro Miranda Aguilera

unread,
Feb 2, 2016, 2:45:08 AM2/2/16
to vagra...@googlegroups.com
Hello Christopher.

What you can do, is use the IDE controller for your iso, so you will now is always IDE.

However, you may hit the same issue,

depening on the OS where the box was created, it could be

SATA Controller or SATA

so maybe the ide would be

IDE Controller vs IDE

If the box is linux, you should be able to mount the iso just fine.

Do a shared folder to path/to/

then on the OS:

mkdir -p /mnt/dvd
mount -o loop,ro /path/file.iso /mnt/dvd

<magic>

umount /mnt/dvd

Hope this helps
Alvaro.


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/890ada33-53a2-4950-bdf9-72c951193c32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christopher

unread,
Feb 9, 2016, 5:19:12 PM2/9/16
to Vagrant
Thanks your help Alvaro.  Sorry for the late response on this but your response got me headed in the right direction.  Your solution specifically mentions Linux.  I am on Windows so I figured I would put my solution up as well.

I took what you said and adjusted my Vagrantfile and provisioners to mount one .iso at time.  Once the application is finished uninstalling, I unmount the .iso and then mount the next one.

There was a gotcha though.  I had to copy over the .iso file to a temporary folder first so that I could properly mount it.  I also did it in a way where I did not have to target a specific drive letter.

Here's the relevant portion of my Vagrantfile:

Vagrant.configure(2) do |config|
...
 
# =================================================================
 
# PROVISIONING
 
# Additional provisioners such as Puppet, Chef, Ansible, Salt, and Docker are also available.
 
# Please see the Vagrant documentation for more information about their specific syntax and use.
 
# =================================================================
  config
.vm.provision "file", source: "C:/Hashicorp/Vagrant/provisioners/install-visualstudio.ps1", destination: "C:/tmp/vagrant-shell/install-visualstudio.ps1"

 
# Provision Visual Studio
  config
.vm.provision "shell", inline: "C:/tmp/vagrant-shell/install-visualstudio.ps1", run: "always"
...
end

And here's my install-visualstudio.ps1 file:

$vspath = "C:\Program Files (x86)\Microsoft Visual Studio 12.0"
if (test-path $vspath)
{
 
Write-Host "Visual Studio is already installed...we can skip this step."
}
else {
 
Write-Host "Installing Visual Studio 2013 with Update 5..."
 
Write-Host "This will take a while so maybe grab some coffee or tea?"
 
Write-Host "Copying ISO file to local file system"
 
Copy-Item "\\VBOXSVR\ISO_Installers\Microsoft\Visual Studio\2013 with Update 5\en_visual_studio_ultimate_2013_with_update_5_x86_dvd_6815896.iso" -Destination "C:\tmp\"

 Write-Host "
Mounting the ISO file"
 $iso = "
C:/tmp/en_visual_studio_ultimate_2013_with_update_5_x86_dvd_6815896.iso"
 Mount-DiskImage -verbose -ImagePath $iso -StorageType ISO

 # Get the drive letter that the ISO was mounted to so that we can access the installer.
 $image = Get-DiskImage -ImagePath $iso | Get-Volume
 $drive = "
$([string]$image.DriveLetter):"

 $p = New-Object System.Diagnostics.Process
 $pinfo = New-Object System.Diagnostics.ProcessStartInfo("
$drive\vs_ultimate.exe", "/full /quiet /norestart");
 $p.StartInfo = $pinfo;
 $start = Get-Date -displayhint time
 $p.Start();
 $p.WaitForExit();
 $end = Get-Date -displayhint time
 $time = NEW-TIMESPAN -Start $start -End $end
 Write-Host "
Visual Studio 2013 with Update 5 installed.  Time taken (minutes): " $time.TotalMinutes
 Write-Host "
Dismounting the Visual Studio ISO file..."
 Dismount-DiskImage -ImagePath $iso
}

Write-Host "
Deleting the Visual Studio 2013 with Update 5 ISO file..."
$file = "
C:/tmp/en_visual_studio_ultimate_2013_with_update_5_x86_dvd_6815896.iso"
if (Test-Path $file){
 Remove-Item $file
}


Write-Host "
install-visualstudio.ps1 complete..."

You'll notice that even if the provisioner runs every time, it does not attempt to install Visual Studio if it is already completed.

Hope this helps anyone else that comes across the same issue.

Christopher

Alvaro Miranda Aguilera

unread,
Feb 12, 2016, 4:07:14 AM2/12/16
to vagra...@googlegroups.com
Hello Christopher,

Couple of comments here.

1. I have seen lots of people use chocolatey to install software, I think they got MS VS there too,
2. Once the VM is installed, you should think into either package the box and create your own box with MS VS Installed, so you save time on the next run
or, you can create your own box with MS VS installed.

the box it self will be a bit larger, but having the software on it, will make it easier to run :)

For 2.a (chocolatey on vagrant) have a look at this:


For 2.b, (packer)

you can see the installer of chocolatey here:

And a sample project here:

Hope this helps
Alvaro.

Christopher

unread,
Feb 13, 2016, 12:35:37 AM2/13/16
to Vagrant
Thanks Alvaro.  Always a great help.

I've definitely considered everything you're saying but I'm doing most of my work for an enterprise using our MSDN license for the software installs (developer boxes with enterprise Visual Studio and SQL Server).

I know I can make a private box up on Atlas with only certain people having access (that's my understanding at least) but I'm trying to do a one time deploy for my team.  We generally don't build up and tear down boxes so it's not a terribly big issue with the install times and the time gained from a standardized configuration is already good enough.

Ideally, what you suggest about building a box on top of a base one is what I want to do but I gotta take baby steps since I'm trying to influence and change the working culture.  "VMs as cattle" (VAC?) is barely starting to become part of our vocabulary in my organization.  Once I've got this box created I probably start work on your suggestion.

Thanks for the push in the right direction!

Regards,

Chris

Christopher

unread,
Feb 15, 2016, 3:01:08 PM2/15/16
to Vagrant
Alvaro,

Thanks again for your direction.  I started testing out the private feature of Atlas' boxes and am going to do what you suggested.  I'll keep my public box public still but for my developers we'll have a private box that is fully loaded and ready to go.

Christopher

unread,
Mar 20, 2016, 5:37:15 PM3/20/16
to Vagrant
Hi Alvaro,

I tried going with your advice a while back and got side tracked with some other projects.  I was never able to get the provisioned box working.  Essentially, I have done up to the following and then I hit a boot timeout error with Vagrant:

  1. Create base box with Windows Updates and upload up to Atlas
  2. Get base box from Atlas and provision Visual Studio, SQL Server, and some other default applications for the VM
  3. Use VBoxManage Export to create an .ova file and store it locally on my host
  4. Use Packer to package up my .ova file with software installed and store it locally
  5. Use VagrantFile to access the newly created box from Packer
  6. vagrant up gets to "Waiting for machine to boot." and times out
I tried increasing the boot timeout to 3600 but it didn't matter.  Not sure what's going on at this point but it doesn't seem like WinRM is able to communicate.  

My WinRM address shows 127.0.0.1:55985 and vagrant user is "vagrant".

Any help is greatly appreciated.
Reply all
Reply to author
Forward
0 new messages