Conifgure VBoxManage commands that require VM be running from Vagrantfile - vagrant

There are several commands under the VBoxManage umbrella that require the guest machine already be running (e.g. controlvm, guestcontrol) but all the vbox configuration I see in the Vagrantfile happens before the machine has started.
Is there a way in the Vagrantfile to configure the machine after it has started?

The goal of vagrant is to abstract the creation of VM for multiple provider (including VirtualBox) it is not really to provide all manipulation of the VM afterwards and replace the VBoxManage -
If you need to run command after the VM has started and run it from Vagrantfile, I would suggest to look at the vagrant-triggers plugin which :
Allow the definition of arbitrary scripts that will run on the host before and/or after Vagrant commands.

Related

How to run gui application on windows vm?

I have installed vagrant and virtual box on my Mac. I have created a Windows10 VM and it's configured with winrm.
I am able to run commands on Windows VM through vagrant. However I am not able to see any GUI on the VM.
For example, if I open command prompt in Windows VM and issue command "start chrome.exe", it launches the chrome browser and browser ui is displayed. However if I type the same command through winrm vagrant winrm -c "start chrome.exe", it launches the browser, but ui is not displayed in VM.Same issue happens if I run commands through shell provisioner.
Is there any way, I can run commands from vagrant and the application will be launched in GUI mode in VM?
Is there any way, I can run commands from vagrant and the application will be launched in GUI mode in VM?
No.
From https://msdn.microsoft.com/en-us/library/aa384426(v=vs.85).aspx :
You can use WinRM scripting objects, the WinRM command-line tool, or the Windows Remote Shell command line tool WinRS to obtain management data from local and remote computers ...
winrm is used for Remote Management and does not forward the X window, so no you cannot launch a program like chrome and forward the UI somewhere else.
Your best options to run UI program from your VM :
run from the VM GUI (either by enabling from Vagrantfile or opening the VM from VirtualBox)
running vagrant rdp to login into the VM
The easiest is to run the VM in 'GUI mode' (as opposed to 'headless').
I use VirtualBox from Oracle, which is one of the options easily configured from within your Vagrantfile.
Check out my "Provider-specific configuration" section:
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# # (so we can run the browser)
vb.gui = true
vb.name = "windows10-eval"
# # Customize the amount of memory on the VM:
vb.memory = "2048"
end
When my VM boots I automatically get a GUI which looks exactly as if I was booting a regular Windows machine. This box comes conveniently with chrome already provisioned, but it'd be easy to install it and use it.
Although you can't directly run a GUI app from the WinRM, you can also add a link to your app in the Windows startup so you will ensure the app is ran on system startup.
Add the following in your provisioning script :
mklink C:\Users\vagrant\AppData\Roaming\Microsoft\Windows\"Start Menu"\Programs\Startup\MyApp.link C:\MyApp\\MyApp.exe
shutdown /r /t 1

Is it possible to enable WinRM in a Modern.ie VM or Vagrant Box by repackaging it with Packer?

There are these free like free beer virtual machines (VM) from Microsoft which one can download from modern.ie and use for testing or whatever. And there is this nice Gist on Github which explains how to enable WinRM support on that VMs. Unfortunately this requires manual interaction with the VM after initial boot up (step 2. in Gist). Is it possible to let Packer do this job using it´s builder type virtualbox-ovf (VIRTUALBOX BUILDER (FROM AN OVF/OVA))? If it is possible can you provide some example code, please.
Or asked another way: How to create a Vagrant Box from an existing VirtualBox image (.ova file) or from an existing Vagrant Box (.ovffile) with Packer?
What you asked is described in Step 4
Package
Since there's a lot of Windows specific configuration, you can include
the Vagrantfile in the package command so winrm and virtualbox
configuration get's default values when the repackaged is used for
other purposes. Remember to run the command in the same directory the
Vagrantfile resides:
$ vagrant package --output "yourboxname" --Vagrantfile Vagrantfile
After that you're all set!
once you complete step2 and 3, you will run step4 which recreate a vagrant box from the updated VM and you can re-use this box
Or asked another way: How to create a Vagrant Box from an existing VirtualBox image (.ova file) or from an existing Vagrant Box (.ovffile) with Packer?
This is not possible, shortly speaking, packer creates Vagrant box from OS ISO distribution, not from existing VM
You can connect to Windows modern.ie VM with ssh (Openssh service runs at startup). You'll have a very limited shell, but enough to call cmd.exe or powershell, and activate WinRM. On Windows 10 VM, you just have to change the network type to something not public. That's it.

Convert vhd to vagrant box?

I have a number of windows VMs running on my hyper-v instance I want to turn into vagrant boxes. Its there a tool out there that can do this for me or a clear guide on what needs to be enabled on the machine and how to create the config files that go into the box?
The documentation for windows VMs coming from hyper-v seem to be lacking with most guides focusing on virtual box.
TIA
There is no direct approach to achieve what you want.
NOTE: remove the hyper-v integration tools (agent, PV drivers etc...).
First, you need to convert VHD to VMDK or VDI format using VBoxManage, for example from VHD to VDI -> VBoxManage clonehd source.vhd target.vdi --format VDI
Once done, create a VM in VirtualBox with a proper spec (# of vCPUs, Memory, etc.), use the existing converted .vdi file as its virtual disk.
Try to boot the VM and see if everything works as expected. Install the VirtualBox Guest Additions (recommended).
Configure the VM as per Vagrant Documentation (e.g. NAT port forwarding rules, disabling UAC etc., refer to Creating a Base Box)
Package it as Vagrant box vagrant package --base vbox_vm_name --output /file/to/name.box

Does the vagrant destroy works on current folder or globally

I am confused with vagrant destroy command.
The output of vagrant status is
Current machine states:
default running (virtualbox)
But I have two vagrant VMs running in Virtual box
MacBook-Pro:server john$ vboxmanage list runningvms
"john-servers_default_1415665580149_91312" {114ad904-8629-4c4a-9344-d685c78a8228}
"test" {a6be5689-0ac3-4ac7-845d-97f2f4022cd9}
Now when I do vagrant destroy, it says do you want to destroy deafult VM
Now I am not sure which machine it will destroy. I am inside test VM but I don't want to take risk.
I tried this vagrant destroy test or vagrant destroy a6be5689-0ac3-4ac7-845d-97f2f4022cd9 but that didn't work
What is the safe way to delete VM?
To destroy test VM, cd into the test folder and enter vagrant destroy - this will destroy the vagrant environment within this directory. After it is destroyed, the default provider will still be shown when you enter vagrant status, together with a not created (virtualbox) status.
vagrant status only shows one vagrant VM running in virtualbox because this command only shows the state of the underlying guest machine within that directory.
In order to see all vagrant environments, enter vagrant global-status --prune - this brings up the state of all the active vagrant environments no matter what directory you run this command from. You can even destroy a VM that's outside of your current directory by typing vagrant destroy ID_TAG by copying the ID tagged to the unwanted VM.
If you are still unsure if the right VM is destroyed, input vagrant halt within the test directory and test if the right VM is being halted. This is at the very least a reversible action in case the wrong VM is selected. But the initial solution vagrant destroy within the test directory should do the trick.
vagrant destroy would remove, in your case, the "john-servers_default_1415665580149_91312" VM.
Vagrant look the VM ID in the directory containing the Vagrantfile in .vagrant/machines/default/virtualbox/id
If you want to remove the "test" VM, you can use VBoxManage.
VBoxManage unregistervm a6be5689-0ac3-4ac7-845d-97f2f4022cd9 –delete
Keep in mind that the -delete flag will remove all disks associated with this VM

Configure TCP endpoint when vagrant up or reload

I'm using Vagrant to run an Ubuntu VM with Microsoft Azure provider, but I need to create an endpoint on this VM.
I've done it installing azure command-line tools with
npm install azure-cli -g
and after that
azure vm endpoint create {VM-Name} 80 8080
VM is running fine without problems. But I don't want to configure the endpoint manually anymore. I would like to create it when I run vagrant up. I don't want to use vagrant provision, because this shell instruction should not be executed in my VM, but in my physical machine.
There is no options such as verbose in vagrant, which would have permitted me to see what it really does with the Vagrantfile, and what I should do to implement a shell command like this one in it.
Ansver to initial question, do the following:
in vagrantfile add following string in azure provider section:
azure.tcp_endpoints = '80:8080'
hope this help.
answer to your question about verbose output:
you may run vagrant up --provider=azure --debug
for additional debug information.
if you need to run local scripts you may try to use vagrant-triggers plugin

Resources