Vagrant series I: Installing and using Vagrant on Windows 8.1 (Hyper-V required)
I’m sure you’ve heard about Vagrant at least to some degree somewhere. If you haven’t it may be time for you to become at least aware of its existence. I will not make any introduction post on it as it has been explained extensively and very well in lots of blogs and Internet pages:
In this post I will describe how I set up my own vagrant environment on Windows 8.1 so it might not fit well in other versions of Windows (Specially if you don’t have the Windows 8 Pro version) which don’t have the Hyper-V feature. My “lab” requirements are:
- Windows 8.1 Pro
- Hyper-V Feature enabled
- Vagrant 1.7.2
- Vagrant Putty plugin
- Atom editor: For editing diferent files and formats such as this post
- Administrator permissions on the host machine
- An existing Hyper-V vSwitch, either Internal or External.
- Putty Solarized Theme
Installing and running Vagrant
Installing vagrant is a pretty easy task in Windows (as well as in Linux), you just have to follow the installation wizard and you’ll be all set. Afterwards if you don’t have Git or some similar UNIX-like simulation environment on Windows, you’ll need to install the vagrant putty plug-in, which in my opinion makes life much easier and nicer than the embedded ssh binary.
After Vagrant is installed, you can access it running powershell (just remember to run it as administrator) and typing
PS C:\Users\Marc> vagrant version Installed Version: 1.7.2 Latest Version: 1.7.2 You're running an up-to-date version of Vagrant!
Aftwerwards we can add an existing box using (it can be remote or local Vagrant will add it anyway)
vagrant box add <box>:
PS C:\Users\Marc> vagrant box add serveit/centos-7 ==> box: Loading metadata for box 'serveit/centos-7' box: URL: https://atlas.hashicorp.com/serveit/centos-7 ==> box: Adding box 'serveit/centos-7' (v1.3.1) for provider: hyperv box: Downloading: https://vagrantcloud.com/serveit/boxes/centos-7/versions/1.3.1/providers/hyperv.box box: Progress: 100% (Rate: 6648k/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'serveit/centos-7' (v1.3.1) for 'hyperv'! PS C:\Users\Marc>
it’s not a requirement, but we can also set the default Vagrant provider using environment variables (local or global). If we don’t set it as an environment variable it’s necessary to set it up in the Vagrantfile(I’ll use t the Vagrantfile method) or when using the
vagrant up <box> --provider <provider> command more info on vagrant documentation
Once we’ve set that, we’ll have to create a folder in which we can initialize our Vagrant environment using
vagrant init and after editing the Vagrantfile created by the
vagrant init command:
PS C:\Users\Marc> mkdir vagrant-centos7 Directory: C:\Users\Marc Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 23/02/2015 23:58 vagrant-centos7 PS C:\Users\Marc> cd .\vagrant-centos7 PS C:\Users\Marc\vagrant-centos7> vagrant init A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
PS C:\users\marc\vagrant-centos7> atom .\Vagrantfile # Vagrantfile vagrant.configure(2) do |config| config.vm.box = "serveit/centos-7" config.vm.provider "hyperv" end
NOTE: THERE IS AN BUG IN VAGRANT 1.7.2 WHICH DOESN’T RESPECT THE SECURE BOOT FLAG WHEN IT IMPORTS THE VM, TO FIX IT PLEASE MODIFY C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.7.2\plugins\providers\hyperv\scripts\import_vm.ps1 TO MATCH THIS
github bug. It’ll be fixed in upcoming versions so no need to worry :) Alternatively, and if you’re running version 1.7.2, replace the file with this one
After setting up our Vagrantfile we will be able to run the vagrant box or VM using
vagrant up It may prompt us for our username and credentials in order to map the SMB share:
PS C:\users\marc\vagrant-centos7> vagrant up Bringing machine 'default' up with 'hyperv' provider... ==> default: Verifying Hyper-V is enabled... ==> default: Importing a Hyper-V instance default: Cloning virtual hard drive... default: Creating and registering the VM... default: Successfully imported a VM with name: CentOS 7 ==> default: Starting the machine... ==> default: Waiting for the machine to report its IP address... default: Timeout: 120 seconds default: IP: 192.168.1.53 ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 192.168.1.53:22 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if its present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Preparing SMB shared folders... default: You will be asked for the username and password to use for the SMB default: folders shortly. Please use the proper username/password of your default: Windows account. default: default: Username: marc default: Password (will be hidden): ==> default: Mounting SMB shared folders... default: C:/Users/Marc/vagrant-centos7 => /vagrant
I like to connect to the newly booted VM using Putty, so instead of typping
vagrant ssh, we’ll install Install Vagrant putty to connect to our newly created Hyper-V VM and copy the putty executable on the windows PATH (C:\Windows for example) to be able to type
PS C:\users\marc\vagrant-centos7> vagrant plugin install vagrant-multi-putty Installing the 'vagrant-multi-putty' plugin. This can take a few minutes... Installed the plugin 'vagrant-multi-putty (1.4.4)'! PS C:\users\marc\vagrant-centos7> vagrant putty
Note that if we have not converted the SSH key that ships with the VM (in OpenSSL format) to a putty .ppk compatible format, we’ll not be able to connect to the machine password-less, instead we’ll have to type
vagrant as a password
A new window will open and we’ll be able to do whatever we want inside our newly booted vagrant instance.
To stop it, we can either type
poweroff inside the instance or type vagrant halt in our powershell prompt.
There are a few commands that are useful when interacting with vagrant:
vagrant upstarts and provisions the vagrant environment
vagrant destroystops and deletes all traces of the vagrant machine
vagrant statusoutputs status of the vagrant machine
vagrant suspendsuspends the machine
vagrant resumeresume a suspended vagrant machine
vagrant haltstops the vagrant machine
vagrant versionprints current and latest Vagrant version
vagrant ssh-configoutputs OpenSSH valid configuration to connect to the machine
vagrant helpshows the help for a subcommand