How to Create a Windows 8.1 Vagrant Box

With the release of version 1.6 Vagrant officially supports Windows. However, there are some gotchas in getting a Windows 8.1 base box up and running with VirtualBox. This tutorial guides you through the necessary steps. It even gives instructions on how to setup SSH access to that Windows box.

I assume that you have VirtualBox and Vagrant 1.6 already installed.

Creation of the Base Box

First of all we have to create a Windows 8.1 virtual machine and configure a few things to make winrm and remote desktop access possible.

  1. Create a new virtual machine in VirtualBox with the following settings:
    • Give it the name Windows 8 Base (or chose some other name and adjust following commands accordingly). Setting the name of the virtual machine.
    • 1024MB of RAM (The base box should not use a lot of resources, but a lower value will result in an awefully slow machine.) Setting the amount of RAM of the virtual machine.
    • A dynamically allocated VDI image. I chose 25GB which fits the approximately 10GB of the Windows installation and leaves some additional space. Creating a virtual hard drive. Creating a virtual hard drive. Creating a virtual hard drive. Creating a virtual hard drive.
  2. Windows 8.1 requires to enable an experimental CPU feature in VirtualBox. Run the following command in a terminal:

     VBoxManage setextradata "Windows 8 Base" VBoxInternal/CPUM/CMPXCHG16B 1
    

    If you are using OS X, you might have to use the full path /Applications/VirtualBox.app/Contents/MacOS/VBoxManage to VBoxManage.

  3. Start the virtual machine in VirtualBox. You will be asked to select the image of the installation DVD. Choose your Windows 8.1 installation DVD image.
  4. Follow the Windows 8.1 installation instruction and make the following choices when asked:
    1. Do not do a upgrade, but select “Custom: Install Windows only”. Custom install.
    2. Create single partition (a second recovery partition is created automatically). Create partition. Create partition.
    3. Chose some PC name which is not vagrant (it is not possible to create a user with the same name as the PC name). Choose PC name.
    4. When asked to select the type of network, chose non-public network. Choose non-public network.
    5. Instead of signing in to your Microsoft account click “Create a new account” and then “Sign in without a Microsoft account”. Create a new account. Sign in without a Microsoft account.
    6. Create user vagrant with password vagrant.
  5. After the installation is complete boot the Windows 8.1 machine. Once up and running select “Devices -> Insert Gust Additions CD image …” from the VirtualBox menu.
  6. Run VBoxWindowsAdditions(.exe) from the now mounted CD image and follow the instructions.
  7. After another reboot open the Desktop and open the system settings (move mouse cursor to lower right corner, select “Settings” and “Control Panel”). Next open “System and Security”, “System”, “Advanced system settings”. Select the “Remote” tab and choose “Allow remote connections to this computer”. Also, uncheck “Allow connections only from computers running Remote Desktop with Network Level Authentication”. Apply the changes and close the settings windows. Setting up winrm.
  8. Goto C:\Windows\System32.
  9. Right click cmd.exe, choose “Run as administrator” and confirm.
  10. Execute winrm quickconfig and confirm the two questions with ‘y’.
  11. Execute the following lines:

    winrm set winrm/config/client/auth @{Basic="true"}
    winrm set winrm/config/service/auth @{Basic="true"}
    winrm set winrm/config/service @{AllowUnencrypted="true"}
    
  12. Open the system settings (move mouse cursor to lower right corner, select “Settings” and “Control Panel”). In the system settings navigate to “System and Security”, “Administrative Tools”, and open “Services”. Open the “Properties” in the context/right-click menu of “Windows Remote Management” and set the “Startup type” to “Automatic”. This is required to have Vagrant automatically detect that the system booted. Setting WinRM startup type to "automatic".

Puppet

If you want to use Puppet (or some other provisioning tool), install it now.

SSH

If you want to have SSH access to your Windows box, follow the these steps; otherwise skip to the next section.

  1. Download and open the Cygwin installer.
  2. Choose to “Install from Internet” and for “All Users”.
  3. On the package selection screen search for openssh and mark it for install. Also select any other packages you want to install.
  4. After the installation is complete right click the link to the Cygwin terminal (should be on your Desktop) and select “Run as administrator”.
  5. Run ssh-host-config and answer the questions with:
    • Should privilege separation be used? yes
    • New local account ‘sshd’? yes
    • Do you want to install sshd as a service? yes
    • Enter the value of CYGWIN for daemon: [] Leave empty and just press enter.
    • Do you want to use a different name? no
    • Create new privileged user account ‘cyg_server’? yes
    • Please enter the password: Chose some password.
  6. Execute the following lines:

     mkdir ~/.ssh
     echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key' > ~/.ssh/authorized_keys
    

    This will enable login without a password when typing vagrant ssh.

  7. Open the system settings (move mouse cursor to lower right corner, select “Settings” and “Control Panel”). Next open “System and Security”, “Windows Firewall”, “Advanced Settings”. Go to “Inbound Rules” and click “New Rule …”. For that rule choose “Port”, “TCP” and a “Specified local port” of 22. On the next pages select “Allow the connection” as well as “Domain” and “Private”. Give the rule a name like “SSH”. Firewall settings. Port rule. Set port. Allow connection. Range of rule. Name of rule.

Pack the Base Box

  1. Shutdown the Virtual Machine.
  2. Run vagrant package --base "Windows 8 Base" -o win8.box.

Use the Base Box

To use the finished Windows 8.1 Vagrant base box use this Vagrantfile template:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "win8"
  config.vm.box_url = "./win8.box"  # Adjust path to the location of your base
                                    # box.
  config.vm.communicator = "winrm"
  # If you did not install SSH, you can remove the following line.
  config.vm.network "forwarded_port", host: 2222, guest: 22
  # Port forwarding for remote desktop.
  config.vm.network "forwarded_port", host: 3389, guest: 3389

  # further settings ...
end

To bring your Vagrant box up use vagrant up.

You can connect to it with vagrant rdp. Unfortunately, this might not work with Vagrant 1.6.3. In that case you have to launch your remote desktop client manually. I do that with rdesktop 127.0.0.1:3389.

If you installed SSH, you can also do vagrant ssh.