Creating a Windows Box with Vagrant 1.6

The latest Vagrant release added out-of-the box support for Windows VMs.  Below are my notes on creating a new vagrant box, and then using WinRM and RDP to connect to a Windows 2008 Server VM.

Pre-requisites:

  • VirtualBox 4.3 or above Installed
    Add installation path (“C:\Program Files\Oracle\VirtualBox”) to your PATH environment variable
  • Windows ISO file
  • Vagrant 1.6.3 or above Installed

Create a VirtualBox Windows VM

The instructions use the command line interface. You are welcome to use the VirtualBox GUI as there’s really no difference in the end result.  You can also skip this if you already have an operational Windows VM in VirtualBox.  See this post on “Create/Manage VirtualBox VMs from the Command Line” and VirtualBox VBoxManage documentation.

Alternatively you could also get a base box from packer-windows (it only configures SSH currently, but they are working on adding WinRM support).

  1. Create/Register the VM
    C:\>VBoxManage createvm --name "w2k8_r2_base" --ostype Windows2008_64 --basefolder C:\VMs\ --register

    Use VBoxManage list ostypes In order to get the possible OS types you can use.  Also note that 64bit OS may not show up on this list if Virtualization Technology (Intel VT-x, AMD-V, etc) is not enabled in the BIOS.

  2. Configure VM according to your needs
    C:\>VBoxManage modifyvm "w2k8_r2_base" --memory 2048 --cpus 2 --vram 128 --acpi on --boot1 dvd --nic1 nat

    Vagrant requires the first network interface to be a NAT adapter

  3. Configure Hard Drive
    C:\>cd VMs\w2k8_r2_base
    C:\VMs\w2k8_r2_base>VBoxManage createhd --filename ./w2k8_r2_base.vdi --size 30000
    C:\VMs\w2k8_r2_base>VBoxManage storagectl "w2k8_r2_base" --name "SATA" --add sata
    C:\VMs\w2k8_r2_base>VBoxManage storageattach "w2k8_r2_base" --storagectl "SATA" --port 0 --device 0 --type hdd --medium ./w2k8_r2_base.vdi
    
  4. Configure DVD Drive and point to your installation ISO file
    C:\VMs\w2k8_r2_base>VBoxManage storagectl "w2k8_r2_base" --name "IDE" --add ide
    C:\VMs\w2k8_r2_base>VBoxManage storageattach "w2k8_r2_base" --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium C:\ISOs\Windows2008\W2K8_R2_64bit.iso
    
  5. Install Windows OS
    Using VirtualBox GUI start the newly created VM and install/setup Windows according to your needs.
  6. Detach the Installation ISO file
    C:\VMs\w2k8_r2_base>VBoxManage storageattach "w2k8_r2_base" --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium none
  7. Install Guest Additions
    C:\VMs\w2k8_r2_base>VBoxManage storageattach "w2k8_r2_base" --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium "C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso"

    Using VirtualBox GUI, open the DVD drive on the Guest VM and install VirtualBox Guest Additions. After you’re finished detach the ISO using the command on the previous step.

Configure WinRM & RDP

  1. Setup WinRM for remote management on Guest VM
    By default WinRM might not be configure for remote management on your newly created Windows VM, the following command enables it.

    C:\>winrm quickconfig -q
  2. Update WinRM config on Guest VM
    I used the following settings (documented on the knife windows chef plugin)

    C:\>winrm set winrm/config/winrs @{MaxMemoryPerShellMB="300"}
    C:\>winrm set winrm/config @{MaxTimeoutms="1800000"}
    C:\>winrm set winrm/config/service @{AllowUnencrypted="true"}
    C:\>winrm set winrm/config/service/auth @{Basic="true"}
    
  3. Enable Remote Desktop
    On Guest VM, Remote desktop can be enabled by going to “My computer” properties, click on “Remote Settings”, and select “Allow connections from computers running any versions for Remote Desktop”
  4. Ensure Windows Firewall Allows WinRM/RDP traffic
    On Guest VM, open “Windows Firewall with Advance Security” and make sure that the Inbound Rules for “Windows Remote Management (HTTP-In)” and “Remote Desktop (TCP-in)” are enabled.

Create a Vagrant Box

  1. Shutdown the Guest Windows VM
  2. Package the Windows Box
    C:\>cd VMs\VagrantBoxes
    C:\VMs\VagrantBoxes>vagrant package --base w2k8_r2_base --output w2k8_r2_base.box
  3. Add the box to Vagrant
    C:\>vagrant box add w2k8_r2_base C:\VMs\VagrantBoxes\w2k8_r2_base.box

Vagrant Up

  1. Initialize Box
    C:\>cd VMs\VagrantVMs\w2k8_r2_vm
    C:\VMs\VagrantVMs\w2k8_r2_vm>vagrant init w2k8_r2_base
  2. Edit Vagrantfile
    By default winrm is not setup (ssh is the default); you need to explicitly set it.  In addition, I didn’t use the default vagrant user/password, so I set those as well.  Lastly make sure to forward the RDP port.

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
     config.vm.box = "w2k8_r2_base"
     config.vm.communicator = "winrm"
     config.winrm.username = "Administrator"
     config.winrm.password = "yourpassword"
     config.vm.network "forwarded_port", host: 33389, guest: 3389
    end
  3. Vagrant Up!
    C:\VMs\VagrantVMs\w2k8_r2_vm>vagrant up
  4. Remote-in to your new Vagrant-managed VM
    C:\VMs\VagrantVMs\w2k8_r2_vm>vagrant rdp

    * In Vagrant version 1.6.3 there was a regression introduced around the rdp command (See issue #3973).  I used my regular Remote Desktop client manually and all worked fine.

At this point you should have a Windows Box all set with Vagrant.  In a future post, I intend to discuss provisioning software with Chef.  If you ran into any issues following these instructions, please add those as comments below.  Thanks for reading!

Advertisements