DevOps Monkey Goodness

How to Provision Windows 2008 Using Vagrant With Windows 8

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.

In this article we’ll setup Virtual Box with Vagrant v1.1.5.  As of writting the vagrant-windows plugin currently only works with v1.1.X and doesn’t support v1.2.X yet.

VeeWee Or Manual Setup

There is a neat addon out there called VeeWee which helps even more when it comes to automating the Windows 2008 setup by using unattended xml files but I found it impossible to going with all sorts of errors.  In the end I found it quicker to setup a base box from scratch which is not ideal but until VeeWee works with the other Vagrant correctly on Windows then it will have to wait until I have more patience. 

Setup Local Environment (Windows 7 & 8)

  1. Vagrant relies on Virtual Box which can be downloaded from here:
    https://www.virtualbox.org/wiki/Downloads 
  2. Download and install Vagrant v1.1.5 from here: 
    http://downloads.vagrantup.com/tags/v1.1.5
  3. You have two options here to get the Ruby runtime on your machine:
    a) If you are planning on running Chef recipes locally then you can install the Chef client from here http://wiki.opscode.com/display/chef/Installing+Chef+Client+on+Windows
    OR
    b) Install Ruby http://rubyinstaller.org/downloads/
  4. Install Vagrant-Windows from the command line: 
    PS C:\Windows\system32> gem install vagrant-windows
    Fetching: gssapi-1.0.3.gem (100%)
    Fetching: nokogiri-1.5.9-x86-mingw32.gem (100%)
    Fetching: httpclient-2.2.0.2.gem (100%)
    Fetching: rubyntlm-0.1.1.gem (100%)
    Fetching: uuidtools-2.1.3.gem (100%)
    Fetching: builder-3.2.0.gem (100%)
    Fetching: nori-1.1.5.gem (100%)
    Fetching: rack-1.5.2.gem (100%)
    Fetching: httpi-0.9.7.gem (100%)
    Fetching: wasabi-1.0.0.gem (100%)
    Fetching: gyoku-1.0.0.gem (100%)
    
    ....
    ....

 

Configure A Windows Base Box in VirtualBox

Follow the step below using Virtual Box to setup a new Windows 2008 base box for later use in Vagrant.  You’ll also need an ISO image for this article.

  1. Launch VirtualBox
  2. Go to Machine -> New 

  3. Configure the amount of memory you’d like for your new Vagrant box

     

  4. Next choose the virtual drive type

     

  5. Next leave the default for drive type
     
     
  6. Keep the default for size “dynamically created”
  7. You can either keep the default location for “File location and size” or move onto a different drive as I did
  8. Once you have ceated your VM you can tweak the settings as you require, for example assign more CPUs etc

    To Install the OS from an ISO or DVD Drive

  9. In Windows 8 this is really simple, just browse to your ISO and click mount!  In Windows 7 you’ll need to use some like Daemon Tools to mount the image.  If you’ve got a DVD just make sure it’s in the drive and skip this step.
     
  10. Back in Virtual Box start your image and you’ll be prompted to select a drive to boot from, you can either select your local DVD drive or newly mounted ISO Windows 2008 image.  If you miss this step like I did you can do it from the Device menu as follows: (just reset the VM after selecting the host drive)

     
  11. Your VM should now boot from the installation media from which you can follow the steps to install Windows 2008.

    Configure The Base Image

    Once Windows is installed and you have logged on there are some steps that need to be done in order to make Vagrant and Vagrant-Windows work.  This will form your standard base image form which you can later layer the applications and configuration using something like Chef.
     

  12. Enable WinRM with basic auth with the following script:

    set-executionpolicy -executionpolicy remotesigned
    winrm quickconfig -q winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="512"}' winrm set winrm/config '@{MaxTimeoutms="1800000"}' winrm set winrm/config/service '@{AllowUnencrypted="true"}' winrm set winrm/config/service/auth '@{Basic="true"}'
  13. In order to share folders you must install the “Guest Additions” from within Virtual Box:
    – To get this going I had to first go to “Devices” -> “CD/DVD Devices” -> “Remove disk from virtual drive”
    – “Devices”  -> “Install Guest Additions”
  14. If you will be using Chef to manage the configuration then you can add this as part of the base image:
    – Install the Windows Chef Client from http://wiki.opscode.com/display/chef/Installing+Chef+Client+on+Windows
  15. Disable UAC
     
  16. Disable shutdown tracker
    Start -> run gpedit.msc 
     
  17. Disable the Server Manager at startup
    Start ->gpedit.msc
    Computer Configuration -> Administrative Templates -> System -> Server Manager – “Enabled”
    (NOTE: You set it to enabled to prevent it from showing)
     
  18. Disable Complex Passwords
    Start -> gpedit.msc
     
  19. Create a local admin account vagrant/vagrant (set password to not expire and uncheck must change at next login)
  20. Disable any services that you don’t need:
    – Print Spooler
    – Windows Time
    – Background Inteligent Transfer Service
    – Windows Firewall
    – Remote Registry
    – NGEN Services 

Create a Vagrant BaseBox

Now that you have created a base Windows 2008 image we can use vagrant to create a .box Vagrant file: 

  1. Run the following command to package the VirtualBox into a Vagrant box:  
    vagrant package --base "C:\Users\gibbonsd\VirtualBox VMs\win2008\win2008.vbox" --output c:\vagrant\win2008.box
    
    [C:\Users\gibbonsd\VirtualBox VMs\win2008\win2008.vbox] Clearing any previously set forwarded ports...
    [C:\Users\gibbonsd\VirtualBox VMs\win2008\win2008.vbox] Creating temporary directory for export...
    [C:\Users\gibbonsd\VirtualBox VMs\win2008\win2008.vbox] Exporting VM...
    [C:\Users\gibbonsd\VirtualBox VMs\win2008\win2008.vbox] Compressing package to: c:/vagrant/win2008.box
    
  2. Run the following to add the new server:
    vagrant box add Win2008 c:\vagrant\win2008.box
     
    [vagrant] Downloading with Vagrant::Downloaders::File...
    [vagrant] Copying box to temporary location...
    [vagrant] Extracting box...
    [vagrant] Verifying box...
    [vagrant] Cleaning up downloaded box...
    

     

  3. Next we must initilise the server:
    cd c:\vagrant
    md Win2008
    cd Win2008
    vagrant init Win2008
    
     
    A `Vagrantfile` has been placed in this directory. You are no 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. 

     

  4. Now we’re nearly there!  We just need to make some changes to the Vagrant config file:
    – Edit c:\vagrant\win2008\vagrantfile 
  5. Make the following changes by uncommenting out the relevant lines:
    –  config.vm.network :private_network, ip: “192.168.33.1”
    –  In order to make vagrant-windows work under config.vm.box add 
    config.vm.guest = :windows
    –  Uncomment the whole section starting config.vm.provider :virtualbox do |vb|….END
    – Add the following:
    config.vm.network :forwarded_port, guest: 80, host: 8080
    config.vm.network :forwarded_port, guest: 5985, host: 5985
    config.vm.network :forwarded_port, guest: 1433, host: 1433
     
  6. Spin the server up!
    cd c:\vagrant\Win2008
    vagrant up
    
  7. sd
  8. d

Optional Configuration Settings

By default the Vagrant Windows winrm operationtimeout is set to 1800 but you can change this by editing the vagrant file:

config.winrm.timeout = 500

You can see all of the available settings here:
http://www.rubydoc.org/github/BIAINC/vagrant-windows/Vagrant/Config/WinRM

 

Common Errors and Debugging

Debugging

If you need to find out what Vagrant is doing then you can set the following environment setting:
VAGRANT_LOG = DEBUG or VAGRANT_LOG = INFO
 
Vagrant Errors
HTTP response returned from server (401)
 
This is an authentication error  trying to get to the guest server.  Ensure you have run the Powershell script above to allow basic authentication.  With the server running confirm the follow works from the host:
 
winrs -r:localhost -u:vagrant -p:vagrant dir C:\
 

You can reset your Vagrant settings with:
c:\users\[NAME]\.vagrant.d

1 Comment

  1. Jay Jay
    10th April 2013    

    Nice post! thanks.

    Have you tried if the following command can work fine with this setting?
    vagrant ssh
    vagrant halt
    vagrant provision #if, say, using chef_solo

    I am working in progress of creating a windows box, but get stucked with commands above. Appreciated if you can give some insight here.

Leave a Reply